我有两个列表框,我想按顺序为listbox1和listbox2中的每个项添加一个。我的意思是我希望Listbox2显示listbox1项目的上一项值。如何删除或隐藏重复项目,如下所示?
例如;
Listbox1 Listbox2
0 -
1 0
2 1
3 2
4 3
... ...
这是我的代码;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DistanceExample
{
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
i++;
foreach (var item in listBox1.Items)
{
listBox2.Items.Add(item);
}
listBox1.Items.Add(i);
}
}
}
答案 0 :(得分:1)
只需使用Clear方法移除listBox2
中的项目:
private void timer1_Tick(object sender, EventArgs e)
{
i++;
// remove old items
listBox2.Items.Clear();
foreach (var item in listBox1.Items)
{
listBox2.Items.Add(item);
}
listBox1.Items.Add(i);
}