Hello,

I am fairly new to C# so forgive me if this is a simple question but I cannot seem to find anything on this subject (I am proabably not searching for the correct thing)

How would you go about setting form control properties such as a succession of many label's text properties dynamically after an event trigger such as a button click?

For an example of a snippit of code I am toying with...

/*
             * The array is already filled with 7 random numbers from
             * a method called generateRandom()
             * 
             * This code works just fine.
             * I am trying to figure out how I can do this
             * without having to hard code the labels text output
             */

            int[] theArray;

            rng.generateRandom(out theArray);

            labelNum0.Text = theArray[0].ToString();
            labelNum1.Text = theArray[1].ToString();
            labelNum2.Text = theArray[2].ToString();
            labelNum3.Text = theArray[3].ToString();
            labelNum4.Text = theArray[4].ToString();
            labelNum5.Text = theArray[5].ToString();
            labelNum6.Text = theArray[6].ToString();

Instead of having to hard code the label's output, how would I go about doing it automatically?

Thank you in advance for any insight you can provide!

Ok here is what I got...

After dealing with a bunch of other issues I had an idea. I figured I would try the Add method. This is what I came up with

RandomNumberGen rng = new RandomNumberGen(_totalNumbers, _minimumValue, _maximumValue, true);

            int[] theArray;

            rng.generateRandom(out theArray);

            int x = 20;
            int y = 70;
            for (int ir = 0; ir < _totalNumbers; ir++)
            {
                Label labelName = new Label();
                labelName.Name = "labelNum" + ir;
                labelName.Location = new Point(x, y);
                labelName.Height = 13;
                labelName.Width = 25;
                labelName.Text = theArray[ir].ToString();

                this.Controls.Add(labelName);
                x += 30;
            }

That worked great, unfortunatly now I have a new problem :sad:

When you click the button again to display the new set of numbers it doesn't display the new set. It only displays the first set of numbers.

I am sure it is something I am missing...
Any ideas?

It's possible that your custom RandomNumberGen class is not seeded properly, where is the seed set?

_rOckbaer, Thanks for looking!

I don't think that is an issue. It works when I use the first code. Also when I output the first set of numbers (let's say 5 numbers) then change it so that it outputs 7 numbers it will not change the original 5 but show the other 2 numbers.

It is almost like the original numbers that are generated are on top of the subsequent numbers thus not showing them.
So I guess what I need is a way to redraw the form on button click event.

Here is the updated code I am using...

RandomNumberGen rng = new RandomNumberGen(_totalNumbers, _minimumValue, _maximumValue, true);

            int[] theArray;

            rng.generateRandom(out theArray);

            int x = 20;
            int y = 70;
            Label labelName = new Label();
            //labelName.Size = new Size(25, 13);
            labelName.Height = 13;
            labelName.Width = 25;

            for (int ir = 0; ir < _totalNumbers; ir++)
            {
                labelName.Name = "labelNum" + ir;
                labelName.Location = new Point(x, y);
                labelName.Text = theArray[ir].ToString();

                Controls.Add(labelName);
                x += 30;
            }
Member Avatar for iamthwee

Are you clearing the array before you generate the new numbers?

Are you clearing the array before you generate the new numbers?

Yeah, I was. I wasn't at first but that was one of my steps and I caught it then (so it's kinda good that this happened lol)

Anyway, I figured out what to do!!! WOOT!

It was obvious too :eek:

I was forgetting about the BringToFront() Label method.

What was happening is that it would generate the number and display it. But it would do so underneath the existing displayed number, effectivly stacking them one on top of the other.
You add this property to your generated labels then it puts the new one on top, thus displaying it:mrgreen:

So the new code would look like...

RandomNumberGen rng = new RandomNumberGen(_totalNumbers, _minimumValue, _maximumValue, true);

            int[] theArray;

            rng.generateRandom(out theArray);

            int x = 20;
            int y = 70;

            Label labelName = new Label();

            for (int ir = 0; ir < _totalNumbers; ir++)
            {
                Controls.Add(labelName);

                labelName.AutoSize = true;
                labelName.Size = new Size(13, 13);
                labelName.Name = "labelNum" + ir;
                labelName.Location = new Point(x, y);

                // Ah the elusive BringToFront() method!!!
                labelName.BringToFront();

                // Set the text
                labelName.Text = theArray[ir].ToString();

                x += 30;
            }

Whoops!

The code should be...

RandomNumberGen rng = new RandomNumberGen(_totalNumbers, _minimumValue, _maximumValue, true);

            int[] theArray;

            rng.generateRandom(out theArray);

            int x = 20;
            int y = 70;


            for (int ir = 0; ir < _totalNumbers; ir++)
            {
                Label labelName = new Label();
                Controls.Add(labelName);
                labelName.Size = new Size(25, 13);
                labelName.Name = "labelNum" + ir;
                labelName.Location = new Point(x, y);
                labelName.Text = theArray[ir].ToString();

                // Ah the elusive BringToFront() method!!!
                labelName.BringToFront();

                x += 30;
            }
Member Avatar for iamthwee

Are you placing the labels adjacent to one another then? Sorrie I don't have my c# compiler at the mo.

Are you placing the labels adjacent to one another then? Sorrie I don't have my c# compiler at the mo.

Yes, but not directly adjacent to one another.

I thought it was solved (and it sorta was). I now have it so that the text generated is on top of the previously generated text.

However, on a subsequent click, if you have requested an output that is less than the previous time (i.e. it produced and output 5 numbers then you choose 3) the former numbers (the original 2 numbers in the previous example) remain.

Sidenote: I am learning C#/VS 2005. My girlfriend comes up with little ideas and I am making them so that I can have experience. So this is not a critical thing per say.


Here are some screenshots to help visualize what is going on...

-------------------

Screenie #1:
http://www.flickr.com/photos/ddosattack/432761750/

As you can see there is no output yet. It will output 6 numbers as indicated in the upper left numericUpDown control

-------------------

Screenie #2:
http://www.flickr.com/photos/ddosattack/432761760/

So far so good! We dynamically created 6 label controls and output the text.

-------------------

Screenie #3:
http://www.flickr.com/photos/ddosattack/432761780/

AH HAH! As you can see, 3 of the original numbers (in blue) remain! That is no good :sad:
The label and text generation that was required on the click event was successful. It created 3 label controls and assigned text to them.

Ok so I came up with a workaround. I don't like it much. It heavily relies upon setting a fixed amount of "blank" labels to cover up the previously generated labels.

Basically, I created a refresh method that is called for each generate button click. Here's the code...

private void refreshTheForm(int loops)
        {
            int x = 26;
            int y = 58;

            for (int i = 0; i < loops; i++)
            {
                Label labelName = new Label();

                splitContainer1.Panel2.Controls.Add(labelName);

                labelName.Location = new Point(x, y);
                labelName.Name = "labelNum" + i.ToString();
                labelName.Size = new Size(25, 13);
                labelName.Text = ""; // Make the label text empty
                labelName.BringToFront();

                x += 25;
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.