简体   繁体   English

如何制作一个快速程序,该程序遍历C#Windows Form应用程序中的列表?

[英]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.

相关问题 C# 如何为 Windows 窗体应用程序创建安装? - C# How do I create an installation for a windows form application? 如何将控制台应用程序中的变量值传递给 C# 中的 windows 表单应用程序(Visual Studio) - How do i pass a variable value from Console application to windows form application in C# (visual studio) 使用C#更新Windows窗体中的文本框时,如何编写“闪存”效果? - How do I program a “flash” effect when updating text boxes in a windows form with C#? 如何使C#Windows窗体应用程序适合屏幕 - How to make a C# Windows Form Application fit to the screen 如何使用 c# windows 窗体应用程序制作计时器以关闭计算机 - How to make a timer to shutdown computer with c# windows form application C#如何在Windows窗体应用程序中的字符串内部进行基本数学运算 - C# How do I do some basic math inside of a string in my Windows Form Application 如何在Windows10快速访问C#中获取所有目录的列表 - How do you get a list of all directories in Windows10 Quick Access c# 如何通过 C# Windows 应用程序连接并向 MariaDB 数据库发送查询? - How do I connect and send queries to the MariaDB database through a C# Windows application? 如何在C#Windows窗体中创建scrollabe图像列表? - How can I make a scrollabe image list in C# Windows Form? 如何从Windows Form应用程序将ac#变量传递给python脚本 - How do I pass a c# variable to a python script from windows form application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM