我不知道您是否考虑过这一点,但您可以通过以下方式完成此任务。
首先创建一个List 的strings
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{} 块以避免应用崩溃。