| | |
Setting Form Controls Dynamically...
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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...
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!
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...
C# Syntax (Toggle Plain Text)
/* * 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
That worked great, unfortunatly now I have a new problem
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?
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
C# Syntax (Toggle Plain Text)
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

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?
_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...
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...
C# Syntax (Toggle Plain Text)
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; }
Last edited by DDoSAttack; Mar 23rd, 2007 at 5:57 pm.
•
•
•
•
Are you clearing the array before you generate the new numbers?
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...
C# Syntax (Toggle Plain Text)
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; }
Last edited by DDoSAttack; Mar 24th, 2007 at 7:11 am. Reason: Fixed some code...
Whoops!
The code should be...
The code should be...
C# Syntax (Toggle Plain Text)
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; }
•
•
•
•
Are you placing the labels adjacent to one another then? Sorrie I don't have my c# compiler at the mo.
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

The label and text generation that was required on the click event was successful. It created 3 label controls and assigned text to them.
Last edited by DDoSAttack; Mar 24th, 2007 at 6:34 pm.
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...
Basically, I created a refresh method that is called for each generate button click. Here's the code...
C# Syntax (Toggle Plain Text)
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; } }
Last edited by DDoSAttack; Mar 24th, 2007 at 7:54 pm.
![]() |
Other Threads in the C# Forum
- Previous Thread: ADO class
- Next Thread: Pleaasee Helpppp
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox class client combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






