Home and Learn: Intermediate Programming
Stick Figure App - Coding Prep Work
Now that you have a form designed, and know a little bit about Version Control, we can start the programming.
As well as being able to choose a drawing color from the color squares on our form, and the Color Dialog box, we can populate a combo box with system colors. These have names like Alice Blue, Khaki, and Navajo White. There are a lot of them. We can do the loading in the Form Load event.
You should have already created a Form Load event in the previous lesson. If not, click on your form to select it. In the properties area, click on the lightning bolt to display a list of form events. Double click the Load one.
To get at all the colors, including the ones for buttons like ControlDark and ControlLight, there is an enumeration you can use called KnownColor. We'll use a for each loop to go round and grab each color.
In Visual Basic .Net, set up this rather tricky loop in your Form Load event:
For Each inbuiltColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
Next
If you're programming in C#, here's your loop:
foreach (KnownColor inbulitColor in Enum.GetValues(typeof(KnownColor))) {
}
Because KnownColor is an enumeration, you need to type Enum after the keyword in (with square brackets in VB). Enum has a Function or method called GetValues. This returns an array. In between the round brackets of GetValues, you need the values you're trying to get. But, because we're trying to access the values in an enumeration, you need to use GetType (vb) or typeof (C#). In between these round brackets you can add KnownColor.
Tricky stuff! As the code for the loop, you need to convert the KnowColor that is now stored in the variable we've called inbuiltColor. Add this line inside of your loop in VB:
Dim loadColor As Color = Color.FromKnownColor(inbuiltColor)
And this in C#:
Color loadColor = Color.FromKnownColor(inbulitColor);
You can use FromKnownColor to convert the KnownColor enumeration to a Color that you can use.
Now add this line to your loop code, the line that finally places the color in the combo box:
VB
cmbColors.Items.Add(loadColor)
C#
cmbColors.Items.Add(loadColor);
The only difference between VB and C# is the addition of a semicolon at the end in C#.
Run your program and take a look at what you have. Click the dropdown list and you'll see a long list of colors, including the system colors:
However, you might not want all the system colors on the list, the ones right down to Transparent in the image above.
You can get rid of the system colors with a couple of if statements. Here are the ones to add in VB:
If loadColor.IsSystemColor = False Then
If loadColor.Name <> "Transparent" Then
cmbColors.Items.Add(loadColor.Name)
End If
End If
And here they are in C#:
if (loadColor.IsSystemColor == false )
{
if (loadColor.Name != "Transparent")
{
cmbColors.Items.Add(loadColor.Name);
}
}
The outer if statement checks if the color is a system color. If it's not, then the inner if statement is activated. This one just checks if the value is Transparent. If it's not, then we can add it to the combo box.
Notice that we're now saying loadColor.Name. This, not surprisingly, gets you the name of the color.
Here's what your Form Load event should look like in VB:
And here it is in C#:
Run your program again. The top color is now AliceBlue:
What we can do now is to change the background color of the label to match the one chosen from the dropdown list.
There's only one line of code needed to change the background color of our label. In Design View, click on your Combo Box to select it. In the properties area on the right, click the lightning bolt to see a list of events. Locate the SelectedIndexChanged event. Double click this event to create a code stub.
The line to add is this (C# programmers need a semicolon on the end):
labelColor.BackColor = Color.FromName(cmbColors.SelectedItem.ToString())
The Color object has a routine called FromName. We want to convert the SelectedItem from the Combo Box called cmbColors. But it needs to be converted to a string. This all goes between the round brackets of FromName. You can then assign this color to the BackColor property of the label.
Try it out. Select a color from your list. The color you chose should now show up on the label:
If you find it doesn't work, try this line instead:
labelColor.BackColor = Color.FromName(cmbColors.Text)
In the next lesson, we'll set up a Pen object. We'll need this a lot. We'll also get the rest of the Color Group Box to work.
Email us: enquiry at homeandlearn.co.uk