Adding Windows Forms Controls at Run Time

Image Information: For C# and VB NET Students

 

Now that we have written code to get information about an image, we can write the code to get the colors used in an image. Before doing that, we'll add some labels to the Windows Form. We want one for each image color, up to a maximum of 256. You'll be glad to know that you won't be doing this by hand. We can add the labels programmatically when the form loads.

Click on your form to select it. Locate the Form Load event. Double-click to create a code stub. Or simply double-click on a blank area of the form. We're going to loop round and add new labels. We'll set them up to be 30 wide by 30 high. We'll need to position them. For this, we'll just enter some hard-code values. Or form is 800 high so a good start position for the first row of labels is 400. We don't want to position a label if it's is going to be to far to the right, as that would be off the form. So we'll restrict how far to the right they can be positioned.

With that in mind, set up two integer variables as the first lines of code in your Form Load code stub. These will be the offsets for the label positions:

C#:

int offsetX = 0;
int offsetY = 0;

VB Net:

Dim offsetX As Integer = 0
Dim offsetY As Integer = 0

Next, set up two variables to hold the label width and label height:

C#

int labelWidth = 30;
int labelHeight = 30;

VB Net

Dim labelWidth As Integer = 30
Dim labelHeight As Integer = 30

We need a for loop now that goes from 0 to 255. We'll create and add the labels inside the loop. Add this loop in C#:

for (int i = 0; i < 256; i++)
{
}

And this one in VB Net:

For i = 0 To 255
Next

The first thing we can do inside of the loop is to check if the offset is greater than 700. (This is an arbitrary number and you can amend this depending on how wide your form is.) If it is, we can reset the X direction (from left to right). We can also increase the Y direction by one label height, ready for the next row.

Add this if statement to your loop in C#:

if (offsetX > 700)
{

offsetX = 0;
offsetY += labelHeight;

}

And this one in VB Net

If offsetX > 700 Then

offsetX = 0
offsetY += labelHeight

End If

Now we can create the new label and set some properties for it. In C#, you create a new label like this:

Controls.Add(new Label
{
});

The properties we want to add go between the two curly brackets. Note that there's a round bracket and a semicolon at the end.

In VB Net you add a new label like this:

Dim labelNew As New Label With {
}

Controls.Add(labelNew)

This is entirely different to the way labels are added in C#. In VB Net, you need the Dim keyword, followed by a variable name. After As New Label, you need the keyword With followed by a pair of curly brackets. You set your label properties between the two curly brackets. After the final curly brackets, you Add your new Control.

The code to set the properties is the same in C# and in VB Net. The only difference is that you need a dot at the start in VB Net. First, we need a Name property. This is important as we'll need to use the name later. Add this between your curly brackets in C#:

Name = "color" + (i).ToString(),

And this in VB Net

.Name = "color" + (i).ToString(),

Note the dot at the start in VB. The only difference between the two. Note, too, that there's a comma at the end.

But the Name for our label is the word "color" followed by the loop number. So the label names would be this:

color0
color1
color2
etc

Now we can set the Size property (don't forget the dot at the start in VB):

Size = new Size(labelWidth, labelHeight),

So you need the Size Class to set the size of a control. In between the round brackets of Size, you type first a width then a height. These are both integers.

Next, we can set the Location property. Add this (with a dot in VB):

Location = new Point(labelWidth + offsetX, 400 + offsetY),

The Location property is set with the Point Class. In between the round brackets of Point, you need an x value and a y value. Both are integers. The x value is how far to the left you want your control and the y value is how far down you want your control. For us, we want the x value to be the label's width plus our offset value. This will move the labels nicely along from left to right. As the y value, we want to move the label down. We start with a hard-coded value of 400. But you can change this, depending on the height of your own form. To the hard-coded value of 400, we add the offsetY value. This changes each time round the loop, remember, inside the if statement.

Now we can set a default background color. Add this (with a dot at the start in VB):

BackColor = SystemColors.ControlDark,

We're setting a system color for the background. The one chosen here is ControlDark. But this is just so that you can see something when the form loads. You can change this to SystemColors.Control. This will get you the same color as the default background color of the form, so it will blend in nicely and you won't be able to see the labels.

The final property to set is some text. You don't have to add the text, however. It's just so that you can see how many labels have been added. Here's the property to add (again, you need a dot at the start in VB):

Text = (i).ToString()

The thing to note here is that the final property on your list doesn't have a comma at the end.

The final line of code we need is to increment the offset variable. Add this as the final line of code in your loop (without the semicolon in VB):

offsetX += 30;

Your Form Load event should look like this in C#:

C# code to add labels to a Windows Form when the form loads

And this in VB Net:

Visual Basic Net code to add labels to a Windows Form when the form loads

Try it out. Run your form and you should see all the labels at the bottom:

A Windows Form showing lables loaded at Run Time

Now we'll get which colors are in the image and change the background color of each label. We'll do that in the next lesson.

Get Image Colors >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page