简体   繁体   中英

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. 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. And it worked just as I intended: 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? 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.

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.

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).

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM