A picture of a keypad key that says Help!

Visual Basic .NET – Tutorial 1

A picture of Mustang Sally, my dog and friend.

Learning How to Use Controls to Control Form Properies

9/24/09

I am aware of VB.Net and I have been using it for awhile now.  The nice thing about VB.NET is that, now, Microsoft allows you to download a free, express version of Visual Studio as well as individual development environments in the .NET family. VB.NET Express is such an available download. Just do a search on the internet for "free VB.NET Express" in your favorite search engine (make sure the link is to the Microsoft website).

The express version will do almost everything that the full version can do.  While you are at the Microsoft website, browse around a bit to learn those differences.

I have been learning all the new methods and code for VB (I will, from now on, refer to VB.NET as VB).  There is much to learn.  However, most of the BASIC code is the same, just the methods and properties have changed.

You can read plenty of information elsewhere about the changes in VB, but this site is about learning VB, so let's do a project!  Ok, if you have read my other VB 6 articles, we can build on that, otherwise, I suggest you go to my home page and read the 3 basic articles on getting started in VB. Those articles, for the most part, will work with VB.Net, however, I suggest that you limit the number of controls that you drop onto the form. VB.Net has many, many more controls than VB 6. I will be using Visual Studio.Net 2008, but I will try to limit the projects to what you can do with VB.Net Express version, but there may be a few differences. If you are using the express version and my project won't work in that version, send me an email from my Send Me Email link on the home page.

First project is about changing settings for your form using controls on the form. We will begin by starting a new project. With all of these projects, I urge you to name the projects for future reference use so that you can glance at the title to identify the type of project.  I named this project "Changing_Form_Properties_With_Controls" so that I can possibly use the code in a future project and I dont have to open and scan each project to find some code that I know that I have written.

After starting the IDE, widen your form to look something like figure 1. We will set some properties in the Form_Load event, also.

<<Click here to view figure 1, then click the back navigate button to return here.>>

Ok, now for some code to set properties for the form while it is loading. The code below, in the Form_Load event, is to setup some physical properties for your form automatically when the application is starting.

To enter this code, double click directly on the form, (not on one of the other controls).

Copy or enter the following code inside the procedure.

Me.Width = 600
Me.Height = 250
Me.BackColor = Color.FromKnownColor(CType(28, KnownColor))
Trackbar2.Value = 28
Trackbar1.Value = 1
Trackbar3.Value = 1
TextBox1.Text = Color.FromKnownColor(CType(28, KnownColor)).Name

In VB, "Me" always refers to the containing form, in other words, the form that you are writing the code in.  So, the first line is somewhat self explanatory. We are are setting the width of Form1 to 600 pixels wide. The same for the second line, self explanatory. The third line, however, is not so self explanatory, and very different than VB6.

This line is setting the background color of the form. The easier way to set a BackColor is like this:

Me.BackColor=Color.AntiqueWhite

Very unlike VB6, but there is a reason for setting it in this way. The number 28 is the first non-tranparent number-color combination for VB color settings. The first number colors, 1-27, are system transparent colors and will not work on the form unless we write some more code to make it work. In this code example, these are the colors that we will be working with. Don't worry, at this time, what the rest of the coding is doing, just know that this is how you set a color by number instead of the the Color.(colorname) method. The reason that we are using the color by number method will be apparent when we start doing the code for the Trackbar control. That leads me to placing the Trackbar controls onto the form.

In VB 6, these were called Slider controls, but in VB.NET, they are called Trackbar controls.

There are differences between the 2, but we will not discuss the differences, as I said before, we are going forward with VB.Net, and I would rather concentrate on what we have now, and not the past. So from now on, I will stop making references to VB 6 and let you figure out those differences on your own. That will save me from having to re-write what has been written all over the internet about those differences.

With that said and disposed of, lets continue with this project, and just know that we are now learning .NET, not VB 6.

Lets place the controls, like shown. The third Trackbar, place someplace near the right. This one will drop onto the form as a horizontal Trackbar, but then, you go over into the properties panel and find the "Orientation" property and set it to Vertical. The Trackbar will now switch to vertical mode and you can grab it and place it with your mouse, as shown. Place the Textboxes as shown and place labels underneath them and change the label text property to read as shown. Move things around as needed to look similar to the example.

Select the bottom Trackbar2 and set the following properties:

Set Maximum to 167, this is the highest color number, set the Minimum to 28, this is the lowest color number that we will use.  Make sure that the SmallChange property is set to 1.

Next, set the Trackbar1 (above the bottom one) propetries to: Maximum=200, Minimum=1 and make sure SmallChange is set to 1.

Next Trackbar3 (the vertical one):

 Maximum=200, Minimum=1, SmallChange=1.

Ok, more code is needed!  Doubleclick on TrackBar1. Just copy or type the code that is in between the procedure definition and the End Sub statement.

Private Sub Trackbar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Trackbar1.Scroll
Me.Width = 600 + Trackbar1.Value
End Sub

This will reset the width of the form from the original width that we set in Form_Load to 600, plus the numbers in the Trackbar. So, the width of the form will change as you move the trackbar from 600 to 600 plus 200, and then, if you slide the trackbar back to the left, the form will resize to 600 again.

Next, code for Trackbar2. Note what I said about the code for TrackBar1, above.

Private Sub Trackbar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Trackbar2.Scroll
Dim x As Integer = Trackbar2.Value
Me.BackColor = Color.FromKnownColor(CType(x, KnownColor))
TextBox1.Text = Color.FromKnownColor(CType(x, KnownColor)).Name
End Sub

This trackbar is setting a new color number for the backcolor property for the form for each new number that you scroll to on the slider.

And now for the vertical trackbar. It should be self explanatory by now.

Private Sub Trackbar3_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Trackbar3.Scroll
Me.Height = 250 + Trackbar3.Value
End Sub

Ok, lets setup the radio buttons code. Doubleclick on each to set the following code.

Private Sub RadioButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click
Panel1.BackColor = Color.Green
End Sub

Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
Panel1.BackColor = Color.Blue
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
Panel1.BackColor = Color.Red
End Sub

As you can see here, we are using the Color,(colorname) method for setting the panel color, instead of the more code, number color method for the trackbar. The reason that we used the number method for the trackbar is because the Trackbar.Value method returns a number for each small change you scroll with the moving slider. This allows us to change the form back color by referencing the value that the trackbar returns. In turn, this changes the form back color to the corresponding returned value of the trackbar control. I hope this makes some sense to you, because you are going to have trouble understanding much of the control methods and properties as we move forward. Some of them are so far from sounding anything like the english language that you could imagine. So, you will need to catch on to these methods and how they function pretty quickly.

Ok, thats it, now press F5 to build and debug the program. After the form appears, slowly scroll the top trackbar to the right. The form should get wider the farther to the right you go and smaller, back to the original size, as you move to the left. Then, slowly scroll the bottom trackbar to the right and the form should start changing colors for each tick that you move to the right, and the far left texbox should register the current color name for the back color. Likewise for the other 2 textboxes, for width and height, as labeled.

This one was a little bigger than the previous tutorials, but for your first .Net lesson, I hope it is helpful. As always, thank you for reading and if you have questions send me an email using my link on the home page. The complete listing is below.

Public Class Form1

Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Trackbar2.Scroll
Dim x As Integer = Trackbar2.Value
Me.BackColor = Color.FromKnownColor(CType(x, KnownColor))
TextBox1.Text = Color.FromKnownColor(CType(x, KnownColor)).Name
End Sub
Private Sub RadioButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click
Panel1.BackColor = Color.Green
End Sub

Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
Panel1.BackColor = Color.Blue
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
Panel1.BackColor = Color.Red
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Width = 600
Me.Height = 250
Me.BackColor = Color.FromKnownColor(CType(28, KnownColor))
Trackbar2.Value = 28
Trackbar1.Value = 1
Trackbar3.Value = 1
TextBox1.Text = Color.FromKnownColor(CType(28, KnownColor)).Name
TextBox3.Text = Me.Height
TextBox2.Text = Me.Width
End Sub

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Trackbar1.Scroll

Me.Width = 600 + Trackbar1.Value
TextBox2.Text = Me.Width
End Sub

Private Sub TrackBar3_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Trackbar3.Scroll
Me.Height = 250 + Trackbar3.Value
TextBox3.Text = Me.Height
End Sub

End Class

A logo of Ardent Realty Group.

A picture of a young girl using an abacus.