Check Boxes in VB .NET

Check Boxes in VB .NET

Check Boxes in VB .NET

Two more useful controls in the Visual Basic toolbox are the Check box and the Option Button. You use these when you want to give your users a choice of options. We'll add both of these to a new Form, and then combine them with a Select Case statement to read what the user has chosen.


Check Boxes

So start a new project. Locate the Checkbox control in the toolbox, under Common Controls. Double click the control and a Checkbox appears on your new Form

You'll see that the Checkbox has the Text property of CheckBox1 by default, and a Name of CheckBox1. If you were to double click again on the Checkbox icon in the toolbox, the new control would be called CheckBox2.


The problem with this approach is that by double clicking each Checkbox, you have several individual Checkboxes. And if you wanted to move them around you'd have to move each Checkbox separately. There is a way to group all your Check Boxes together, and move them around as one - by using a Group Box. (You can use a Panel control as well, but we'll stick with the Group Box.)

So, click on your Checkbox with the right mouse button. From the menu that pops up, select delete to get rid of it.

Now locate the Group Box control in the toolbox:

A GroupBox in the VB NET toolbox

It's better to draw this one on the form, rather than dragging and dropping. When you've added one, the only thing you should have on your Form is a Group Box.

We're not going to be using many of the Properties in the Group Box Property box. But click on your GroupBox to select it, and change to the Text Property to "Top 5 movies of all time". Change the Font Property to anything you like. You should now have a Form like this one:

A Group Box on a VB .NET form

The Group Box we just added will hold our Checkboxes. It acts as a container for the controls. To move the Checkboxes about, we can just click on the Group Box to select it, and drag and drop the Group Box somewhere else. The Checkboxes will all move with the Group Box. Let's add some Checkboxes to it.

You CAN'T double click a checkbox and add it to a Group Box. The only way to add a control to a Group Box is to draw one on the Group Box.

  1. Click once with your left mouse button on the Checkbox icon in the VB toolbox
  2. Move your mouse pointer over to the inside of the Group Box. The mouse pointer will change to a cross
  3. Hold down you left mouse button inside the Group Box. Keep the button held down, and drag outwards. Release the left button when you're happy with the size. You can always resize it later.
  4. Add 5 Checkboxes to your Group Box
  5. Change the Text property of each of your Checkboxes to any five movies. Your Form should now look something like the one below:

A VB NET form showing 5 checkboxes

Run your programme to test it out. Click inside a Checkbox to select an item. Click again to deselect it. When you've finished, return to the Design Environment and click on the Group Box itself to select it. Make sure the Group Box IS selected, and not one of your Checkboxes. You can now drag the Group Box around your Form and all the Checkboxes will move with it.

The point about having Checkboxes is to offer your users multiple choices. We'll now write some code to get the choices made by the user. All the Checkboxes with ticks inside them will have their Text displayed in a Message Box.


The Checkbox Code

If you click on any one of your Checkboxes and examine its Properties in the Property box, you'll notice that it has a CheckState Property. Click the down arrow to see the options this CheckState has.

The CheckState property

As you can see, you are given three options: Unchecked, Checked, Indeterminate.

If a checkbox has been selected, the value for the CheckState property will be 1; if it hasn't been selected, the value is zero. (The value for the Indeterminate option is also zero, but we won't be using this.)

We're only going to test for 0 or 1, Checked or Unchecked. You can do the testing with a simple If Statement. Like this:

If CheckBox1.CheckState = 1 Then


End If

After you type the equal sign, though, VB will give you a drop down box of the values you can choose from. So the above code is the same as this:

If CheckBox1.CheckState = CheckState.Checked Then


End If

Whichever you choose, the Statement will be True if the checkbox is ticked and False if it isn't.

Add a Button to your Form and put that code behind it (either of the two, or test both). When you've finished typing the code, run your programme. Put a tick inside Checkbox1, and click your button. You should get a Message Box popping up.

Amend your code to this:

If CheckBox1.CheckState = CheckState.Checked Then



MessageBox.Show("Not Checked")

End If

An alternative to Else is ElseIf. It works like this:

If CheckBox1. CheckState = 1 Then


ElseIf Checkbox1. CheckState = 0 Then


End If

When using the ElseIf clause, you need to put what you are testing for on the same line, just after ElseIf. You put the word Then right at the end of the line. You can have as many ElseIf clauses as you want. In other words, it's exactly the same as the first "If" line only with the word "Else" in front "If".


Add 4 more If Statements to check for the values in your other Checkboxes :

Checkbox2.CheckState, Checkbox3.CheckState, etc.

We're now going to get rid of the Message Boxes inside the If Statements. So either comment out all your MessageBox lines, or delete them altogether.

Instead, we'll build up a String Variable. So add a String Variable to the code for your button, and call it message.

The message variable needs to go inside the If Statement. If the user has checked a Box (If its CheckState property is 1), then we build the message. We need to remember what is inside the message variable, so we can just use this:

message = message & Checkbox1.Text & vbNewLine

That way, every time an option is Checked, Visual Basic will keep what is in the variable called message and add to it whatever the text is for the Checkbox.

So add that line to your If Statements. Something like this

If Checkbox1.CheckState = 1 Then

message = message & Checkbox1.Text & vbNewLine

End If

If Checkbox2.CheckState = 1 Then

message = message & Checkbox2.Text & vbNewLine

End If

And at the end of your If Statements, on a new line, add this:

MessageBox.Show("You have chosen " & vbNewLine & message)


Here, we're building a text message for out Message Box. We're saying our Message Box is to contain the text "You have chosen " And a New Line And whatever is inside the variable called message.

When you've finished, Run your Programme to test it out. Put a tick inside all of your Checkboxes. When you click your button all your movies should appear in the Message Box. Check and Uncheck some options, and click the button again. Only those items that are selected should appear in your Checkbox.

So, we can test to see which Check Boxes a user has ticked, and we can keep a record of those choices for ourselves.

What we can also do is count how many Check Boxes were ticked. We can then use a Select Case Statement to display a suitable message.

Keeping a count is straightforward. First we set up an integer variable called counter, and set it's value to zero.

Dim counter As Integer = 0

Then we can just keep adding one to whatever is in this counter variable. In other words, every time a Checkbox has a value of 1 (is ticked), we can add one to our counter (increment our variable).

We can add this straight into our If Statement, on a new line after the message code.

counter = counter + 1

So your code would be this:

If Checkbox1.CheckState = 1 Then

message = message & Checkbox1.Text & vbNewLine
counter = counter + 1

End If

To test that your counter is working, you can add a second message box to the end of the code, just below your first message box:

MessageBox.Show("Counter = " & counter)

Or adapt your first message box:

MessageBox.Show("You have chosen " & counter & " Movies")

Now that we have a way to count how many Checkboxes are ticked, we can add a Select Case Statement.


Exercise I

Add a Select Case Statement to the end of your code to test whatever is inside the variable called counter.

Remember what the format is for a Select Case? It's this:

Select Case VariableName

Case 0

MsgBox "You obviously don't like Movies!"

End Select

If you have 5 Check Boxes on your Form, then the maximum value that counter will hold is 5. So you only need to go up to Case 5.

Add suitable messages for each Case that you're testing for.


This has been quite a long lesson, so give yourself a big pat on theback if you got through it! In the next part, we'll see how to add Option Buttons to a VB .NET form.