简体   繁体   English

如何检测List <string> 被改变了 ? 最后添加的项目是什么?

[英]How can i detect if List<string> was changed ? And what is the last item that was added?

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;
}

The timer is set to 50000 and the time is running all the time over and over again. 计时器设置为50000,时间一直在反复运行。 Now the List<string> myList when i'm running my program has, for example, 3 items: 现在,当我运行我的程序时, List<string> myList有3个项目:

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

After 50 seconds there are two options: either the List not changed or it has changed/longer. 50秒后有两个选项:列表未更改或更改/更长。 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. 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. 第一次运行程序时,检查最近的项目(本例中Index 0处的字符串)是否包含两个单词/字符串。 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; 但是,如果它确实包含单词/字符串并且我们“做某事”,则只在50秒后执行一次; don't do it again even if the words/string exist again in index 0. 即使单词/字符串在索引0中再次存在,也不要再次执行此操作。

  2. After 50 seconds if the List changed and in Index 0 this words/strings exists, do something and again only once after 50 seconds. 50秒后,如果列表发生变化,并且在Index 0存在这样的单词/字符串,则在50秒后再做一次。 If the List didn't change, don't do it again even if the words/string still exist in index 0. 如果列表没有更改,即使索引0中仍然存在单词/字符串,也不要再次执行此操作。

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

I want to start timer3 only if one of the words/strings exist in index 0. 我想只在索引0中存在一个单词/字符串时才启动timer3

If nothing has changed after 50 seconds don't start timer3 again. 如果50秒后没有任何变化,请不要再次启动timer3

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. 只有在50秒或更晚之后,列表才会更改并且其中一个单词/字符串再次存在于索引0中,然后再次启动计时器3。

The Generic List<T> class does not support notifications for list changes. 通用List<T>类不支持列表更改的通知。
What you are looking for is ObservableCollection<T> . 您正在寻找的是ObservableCollection<T>
It has a CollectionChanged that triggered when the collection is being modified. 它有一个CollectionChanged ,在修改集合时触发。

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
        }
}

To detect if your list was changed , check your list item count using : You have to have temp variable containing current list item count : 要检测list已更改,请使用以下方法检查列表项计数:必须包含包含当前列表项计数的temp变量:

int temp = list.Count;// Gets the number of elements contained in the List. int temp = list.Count;//获取List中包含的元素数。

then when you add item: 然后当你添加项目时:

list.add("string one");

then use the temp variable to do like this : 然后使用temp变量做这样的事情:

if(temp != list.Count){
Console.WriteLine("list was changed.");
}

then of course if your list is not sorted: you can get the last item with : 那么当然如果您的列表没有排序:您可以获得最后一项:

list[list.Count - 1]; //last item in the list that was added //添加的列表中的最后一项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测何时添加,删除或更改字体? - How can I detect when a font is added, removed, or changed? 我如何检查清单 <string> 已经改变了? - How can i check if a List<string> have been changed? 如何正确制作对象列表的列表(主要),以便将项目添加到列表时,不会将其添加到列表(主要)中的所有列表? - How can I make a List(main) of Lists of Objects correctly so that when an item is added to a list, it is not added to all the lists in the List(main)? 在组合框中选择最后添加的列表项 - select last added list item in combo box 我需要列表中最后一项的索引,如何? - I need the Index of the last item in a List, how? 我该如何添加到列表中 <string> 目录中的最后一个文件? - How can i add to the List<string> the last file from the directory? 如何获取字符串数组列表的最后一个元素? - How can I grab the last element of a List of string arrays? 将相同项目添加到列表框中时,如何更新时间? - How can I update the time when the same item is added to the list box? 如何检测我的ObservableCollection中的项是否已更改 - How to detect if an item in my ObservableCollection has changed C# 添加新项目时,列表中的项目会发生变化 - C# The items inside the list are changed when a new item is added
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM