[英]How do I make a quick program which goes through a list in a C# Windows Form Application?
I was experimenting with lists in C#'s console application, specifically a randomized int list which had its number order randomized. 我正在C#的控制台应用程序中尝试使用列表,特别是一个随机的int列表,该列表的数字顺序是随机的。 In this experiment I wanted to go through the randomized values from the list when I pressed enter and when it had shown all the randomized values it would stop.
在这个实验中,我想按一下Enter键查看列表中的随机值,当它显示了所有随机值时,它将停止。 And it worked just as I intended: http://i.imgur.com/bNOYrZp.png[ ^]
它按照我的预期工作: http : //i.imgur.com/bNOYrZp.png [ ^]
Random r = new Random();
int tempValue;
List<int> number = new List<int>();
number.Add(1);
number.Add(2);
number.Add(3);
number.Add(4);
number.Add(5);
number.Add(6);
number.Add(7);
number.Add(8);
number.Add(9);
number.Add(10);
for (int i = 0; i < 10; i++)
{
tempValue = r.Next(0, number.Count);
Console.WriteLine(number[tempValue]);
number.RemoveAt(tempValue);
Console.ReadLine();
}
Now how do I do a similar thing in C#'s Windows Form Application? 现在如何在C#的Windows窗体应用程序中执行类似的操作? Instead of pressing enter to go through the list, I press a button to go through the list, and the order of the values are displayed on a label every time I press this button.
我没有按Enter来浏览列表,而是按了一个按钮浏览该列表,并且每次按此按钮时,值的顺序都会显示在标签上。
I used a similar code, but it did not work as intended. 我使用了类似的代码,但是没有按预期工作。 Instead of going through the randomized values it kept making a new order of values which it kept doing every time I clicked the button.
与其遍历随机值,不如每次单击按钮时,它都会按新顺序排列值。 What I want it to do is to go through the randomized values and after it has showed all the 10 randomized values, without duplicates, it stops.
我想要它做的是浏览随机值,并在显示所有10个随机值(无重复)后停止。
private void button1_Click(object sender, EventArgs e)
{
List<int> number = new List<int>();
Random r = new Random();
int tempValue;
number.Add(1);
number.Add(2);
number.Add(3);
number.Add(4);
number.Add(5);
number.Add(6);
number.Add(7);
number.Add(8);
number.Add(9);
number.Add(10);
for (int i = 0; i < 10; i++)
{
tempValue = r.Next(0, number.Count);
label1.Text = number[tempValue].ToString();
number.Remove(number[tempValue]);
}
}
Since you put the list creation and for loop in the click event, its working "as-coded" (obviously not what you intended). 由于您将列表创建和 for循环放在click事件中,因此它的工作方式为“已编码”(显然不是您想要的)。
Remember, the entire click handler runs every time you press the button. 请记住,每次单击按钮时, 整个单击处理程序都会运行。 So you need to initialize the list elsewhere and then just iterate through it on click.
因此,您需要在其他地方初始化该列表,然后在单击时对其进行迭代。 Something like:
就像是:
private Random rng = new Random();
private List<int> numbers = new List<int>();
private void Form_Loaded(...) //Set to Form's Loaded event
{
number.Add(1);
number.Add(2);
number.Add(3);
number.Add(4);
number.Add(5);
number.Add(6);
number.Add(7);
number.Add(8);
number.Add(9);
number.Add(10);
}
private void button1_click(...)
{
tempValue = rng.Next(0, number.Count);
label1.Text = number[tempValue].ToString();
number.Remove(number[tempValue]);
}
Note that this code will have a few issues once the list runs out, there is no way to re-initialize the list, etc. I leave those as an exercise to you. 请注意,一旦列表用完,此代码将出现一些问题,无法重新初始化列表,等等。我将其作为练习留给您。
Also note that I created one instance of Random
and stored it at the class level. 还要注意,我创建了一个
Random
实例,并将其存储在类级别。 In general, you want to use one instance per class to avoid seeding issues (though recreating it in the button click would have technically worked, since you probably can't click the button fast enough). 通常,您希望每个类使用一个实例来避免种子问题(尽管在按钮单击中重新创建它在技术上是可行的,因为您可能无法足够快地单击按钮)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.