【问题标题】:Removing items from listBox over time - C#;随着时间的推移从列表框中删除项目 - C#;
【发布时间】:2017-04-05 08:27:43
【问题描述】:

我目前正在开发一个聊天程序,我的想法是让它成为一个秘密程序(有点像 Facebook 有秘密聊天功能)。

我的消息被发送到listBox 组件,我希望每 10 秒或“n”秒删除最旧的消息。一世 试图用索引标记每条消息,但不太明白它是如何工作的。

我要问的是,你们是否知道一个函数,或者可以帮助我编写一个可以做到这一点的函数。我正在使用 Visual Studio 2015 Windows 窗体,C#。

【问题讨论】:

  • 到目前为止您尝试过什么?请提供一些代码给我们,然后我们可以尝试帮助您改进它。
  • 请添加winform标签。

标签: c# winforms listbox


【解决方案1】:

好吧,当你有一个ListBox 时,所有的项目都会被索引,因为它是一个对象集合(一个对象数组)。从 0 开始向上更新条目。

假设我们向 ListBox 添加 3 个项目

listBox1.Items.Add("Item 1"); //Index 0
listBox1.Items.Add("Item 2"); //Index 1
listBox1.Items.Add("Item 3"); //Index 2

您所要做的就是创建一个在后台运行的线程,每次都会删除索引 0(最旧的条目)处的项目。

new Thread(() =>
{
   while(true)
   {
       if(listBox1.Items.Count > 0) //Can't remove any items if we don't have any.
       {
           Invoke(new MethodInvoker(() => listBox1.Items.RemoveAt(0))); //Remove item at index 0.
           //Needs invoking since we're accessing 'listBox1' from a separate thread.
       }
       Thread.Sleep(10000); //Wait 10 seconds.
   } 
}).Start(); //Spawn our thread that runs in the background.

【讨论】:

    【解决方案2】:

    在 C# WinForms 中,ListBox 包含 ListBoxItems,它们是 ObjectCollection (msdn-link)

    所以你可以添加任何你喜欢的对象,将显示的消息来自于 DisplayMember

    例如

    public class MyMessage {
        public DateTime Received { get; set; }
        public string Message { get; set; }
        public string DisplayString 
        {
            get { return this.ToString(); }
        }
        public string ToString() {
            return "[" + Received.ToShortTimeString() + "] " + Message;
        }
    }
    

    可以添加为 ListBoxItem。

    将 DisplayMember 设置为 "DisplayString" (more here) 将为您提供正确的输出。

    现在您可以遍历 ListBoxItems,将它们转换为 MyMessage 并检查它们的接收时间。

    【讨论】:

      【解决方案3】:

      我不知道您是否考虑过这一点,但您可以通过以下方式完成此任务。

      首先创建一个Liststrings

      List<string> list1 = new List<string>();
      

      要使用列表功能,您必须在表单中包含集合

      using System.Collections;
      

      现在是棘手的部分。

      首先全局声明一个静态整数变量,即在所有类之外。

      static int a;
      

      每当您收到消息时(考虑到您的消息将采用字符串格式),您必须将该字符串添加到您创建的list1

      list1.Add("the received message");    
      

      现在您必须声明一个计时器(如果您是新手,请查看计时器的工作原理)。 Windows 窗体已经有计时器,使用它会更好。 计时器在所需时间后发送一个 Tick 事件。

      private void timer1_Tick(object sender, EventArgs e)
          {
               a = list1.Count() - 1;  //Count will return the number of items in the list, you subtract 1 because the indexes start from 0
               list1.RemoveAt(a);
               listBox.Items.Clear();
               foreach(string x in list1)
               {
                    listBox.Items.Add(x);
               }
          }
      

      这段代码的作用是,在timer 中的每个Tick event 处,它都会刷新列表框,从数组中删除最后一个元素,并用其余元素重新填充列表框。

      要使用计时器,只需将其拖放到表单上即可。它都是基于 GUI 的,很容易弄清楚。

      如果您有任何疑问,请告诉我。

      提示:最大限度地利用 try{}catch{} 块以避免应用崩溃。

      【讨论】:

      • 根据listBox 中项目的顺序,您可能需要使用listBox.Items.Append.Insert。与重新创建整个列表的 foreach 相比,节省了一些时间。
      • @MarkusDeibel 它可以节省时间,但这不是必需的。需要清除列表框,然后在没有最后一条消息的情况下重新填充。你还有别的意思吗?
      • 你能不能只从Items 中删除最后一项并且列表框会反映更改?你真的必须重新填写项目列表才能重绘吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 2015-04-14
      相关资源
      最近更新 更多