你好,首先对不起我的英语不好。我有两个列表框,我想随机放置在其他列表框上,我想让searchString不受随机干扰,我想添加第一个列表框。
private void Form1_Load(object sender, EventArgs e)
{
string[] names = new string[12];
names[0] = "Item 0";
names[1] = "Item 1";
names[2] = "Item 2";
names[3] = "Item 3";
names[4] = "Item 4";
names[5] = "Item 5";
names[6] = "Item 6";
names[7] = "Item 7";
names[8] = "Item 8";
names[9] = "Item 9";
names[10] = "Item 10";
names[11] = "Item 11";
this.LB_1.Items.AddRange(names);
}
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
int number = LB_1.Items.Count;
for (int i = 1; i <= number; i++)
{
//if (LB_1.Items[i].ToString().Contains(searchString))
//{
// LB_2.Items.Add(searchString);
// //i cant add and delete LB_1 seached item
//}
Random rdn = new Random();
int rnd = rdn.Next(0, LB_1.Items.Count);
LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}
}
}
答案 0 :(得分:0)
这是您要寻找的:
private void button1_Click(object sender, EventArgs e)
{
string searchString = "Item 3";
LB_2.Items.Add(searchString);
Random rdn = new Random();
while (LB_1.Items.Count > 0)
{
int rnd = rdn.Next(0, LB_1.Items.Count);
if (LB_1.Items[rnd].ToString() != searchString) LB_2.Items.Add(LB_1.Items[rnd]);
LB_1.Items.RemoveAt(rnd);
}
}