Click here to Skip to main content
15,886,847 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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[^]

C#
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.

C#
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]);
                
            }
            
        }
Posted
Comments
PIEBALDconsult 17-Apr-15 12:56pm    
Move the declaration and initialization of the List and Random out of the method. The Random is best defined as a static field, the List could be a regular member.
Sergey Alexandrovich Kryukov 17-Apr-15 13:10pm    
Good point; I did not pay attention for that.
But the problem itself is much simpler; please see Solution 1.
—SA

1 solution

This problem has little to do with the UI in general and System.Windows.Forms in particular, but you haven't done anything reasonable to show the results, not even remotely. How could you expect that some control like a label would act as a console? You simply assign the value to some property (Text, int this case) in the cycle, but why? In each iteration, old value of the property gets discarded, and you replace it with another value. It's hard to imagine how could you even come to such a weird idea.

But isn't the problem simple. Just to think about it: if you are already using some list, wouldn't it be natural to use some list-related control to present things? You could use System.Windows.Forms.ListBox, as a simplest thing:
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox%28v=vs.110%29.aspx.

Alternatively, by some reasons related exclusively to some graphic rendering subtleties, I often use TreeView instead, with just one level of hierarchy, without any child nodes.

In this way, you can just add (Items.Add) new items:
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items%28v=vs.110%29.aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection%28v=vs.110%29.aspx.

Also, you may want to bind some list to a list box or other control:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.bindingcontext(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.databindings(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.resetbindings(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.onbindingcontextchanged(v=vs.110).aspx.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900