private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
int a = int.Parse(label3.Text)
+ 1;
label3.Text = a.ToString();
if (label3.Text == "10")
{
listBox1.Items.Add(label1.Text);
label3.Text = "0";
}
}
private void timer2_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int num = rnd.Next(1, listBox2.Items.Count);
label1.Text = num.ToString();
if (label3.Text == "10")
{
listBox1.Items.Add(label1.Text);
listBox2.Items.Remove(label1.Text); //this line of code isnt working. It doesnt delete anything.
}
}
我想发生的是我的label1将使用timer1混洗listbox2中的所有项目(1-10),然后,如果计时器达到10秒,listbox1将在其项目上添加label1最后一个数字,listbox2将其删除。它工作正常,但不会删除列表框2中的内容
答案 0 :(得分:0)
您可以使用:
int i = listBox2.Items
.Cast<ListBoxItem>()
.ToList()
.FindIndex(x=>x==label1.Text);
listBox2.RemoveAt(i);
更新:
Random rnd = new Random();
private void timer2_Tick(object sender, EventArgs e)
{
int num = rnd.Next(1, listBox2.Items.Count);
label1.Text = num.ToString();
if (label3.Text == "10")
{
listBox1.Items.Add(label1.Text);
listBox2.Items.Remove(label1.Text); //this line of code isnt working. It doesnt delete anything.
}
}