我如何检测列表<串GT;被改变?什么是添加的最后一个项目? [英] How can i detect if List<string> was changed ? And what is the last item that was added?

查看:104
本文介绍了我如何检测列表<串GT;被改变?什么是添加的最后一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计时器滴答事件:

I have a timer tick event:

private void timer2_Tick(object sender, EventArgs e)
{
    combindedString = string.Join(Environment.NewLine, ListsExtractions.myList);
    richTextBox1.SelectAll();
    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
    richTextBox1.Text = combindedString;
}



定时器设置为50000,时间到了运行的所有的时间和再次。
现在的列表与LT;字符串> myList中当我跑步时我的程序有,例如,3个项目:

The timer is set to 50000 and the time is running all the time over and over again. Now the List<string> myList when i'm running my program has, for example, 3 items:

Index 0: Hello world
Index 1: 24/7/2014 21:00
Index 2: http://test.com

在50秒后有两种选择:要么没有改变列表或它改变/更长。
。如果没有改变什么也不做,但是如果更改获得最新添加的项目。例如,如果列表现在已经改为这个...

After 50 seconds there are two options: either the List not changed or it has changed/longer. If not changed do nothing but if changed get the latest added item. For example, if the list has now been changed to this...

Index 0: This is the latest item added in index 0
Index 1: 24/7/2014 22:00
Index 2: http://www.google.com
Index 3: ""
Index 4: Hello world
Index 5: 24/7/2014 21:00
Index 6: http://test.com

...然后我需要做的另外两个动作:

... then I need to do another two actions:


  1. 在运行程序第一次,检查最近的项目(在 0指数在这个例子中,字符串)包含两个单词/字符串。如果确实如此,那么做一些事情,否则,什么都不做结果
    但是,如果它确实包含单词/字符串,我们做做什么,只有50秒后做一次。不要再这样做,即使单词/字符串中的索引0再存在了。

  1. When running the program for the first time, check if the most recent item (the string at Index 0 in this example) contains two words/strings. If it does, then do something, otherwise, do nothing.
    However, if it does contain the words/strings and we do "do something", only do it once after 50 seconds; don't do it again even if the words/string exist again in index 0.

50秒后,如果列表改变,在索引0 这句话/串存在,在50秒后做一些事情,再一次。如果列表中没有变化,不要再即使单词/字符串中的索引0仍然存在这样做。

After 50 seconds if the List changed and in Index 0 this words/strings exists, do something and again only once after 50 seconds. If the List didn't change, don't do it again even if the words/string still exist in index 0.

if (rlines[0].Contains("צבע אדום") || rlines[0].Contains("אזעקה"))
{
    timer3.Start();            
}


我要开始 TIMER3 索引0仅一家之言/存在串。

I want to start timer3 only if one of the words/strings exist in index 0.

如果一切都没有50秒唐后改变T启动 TIMER3 试。

If nothing has changed after 50 seconds don't start timer3 again.

仅在50秒后或更高版本的名单发生变化,一家之言/字符串索引0存在只能再次然后再次启动定时器3

Only if after 50 seconds or later the List changed and one of the words/strings exist in index 0 again only then start timer 3 again.

推荐答案

普通的列表< T> 类不支持通知名单的变化。结果
你所寻找的是的 的ObservableCollection< T> 结果
它具有的 Col​​lectionChanged 触发集合时,是被修改

The Generic List<T> class does not support notifications for list changes.
What you are looking for is ObservableCollection<T>.
It has a CollectionChanged that triggered when the collection is being modified.

您可以通过以下方式使用它:

You can use it in the following manner:

using System.Collections.ObjectModel;

ObservableCollection<string> myList;

//The cnstructor 
public MyClassname()
{
  this.myList = new ObservableCollection<string>();
  this.myList.CollectionChanged += myList_CollectionChanged;
}

void myList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
        //list changed - an item was added.
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            //Do what ever you want to do when an item is added here...
            //the new items are available in e.NewItems
        }
}

这篇关于我如何检测列表&LT;串GT;被改变?什么是添加的最后一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆