Section Three Exercises

Number of View: 0

Part 1 – If statements

Start a new project. Add a textbox, a Label and a button to your new Form. Then write a programme that does the following:

  1. Asks users to enter a number between 10 and 20.
  2. The number will be entered into the Textbox.
  3. When the Button is clicked, your Visual Basic code will check the number entered in the Textbox.
  4. If it is between 10 and 20, then a message will be displayed.
  5. The message box will display the number from the Textbox.
  6. If the number entered is not between 10 and 20 then the user will be invited to try again, and whatever was entered in the Textbox will be erased

Part 2 – Select Case Statements

Add a Combo box and another button to your form. Create a list of items for your Combo Box. The list of items in your Combo box can be anything you like – pop groups, football teams, favourite foods, anything of your choice. Then try the following:

Use a select case statement to test what a user has chosen from your drop-down list. Give the user a suitable message when the button was clicked.

 

In the next section of this course, we’ll move on to loops in Visual Basic .NET.

VN:F [1.9.10_1130]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.10_1130]
Rating: 0 (from 0 votes)

Popularity: unranked

Conditional Operators

Number of View: 10464

The Conditional Operators allow you to refine what you are testing for. Instead of saying “If X is equal to Y”, you can specify whether it’s greater than, less than, and a whole lot more. Examine the list of Operators:

Operator Meaning
> This symbol means Is Greater Than and is used like this:

If number > 10 Then
MsgBox “The Number was Greater Than 10″
End If

 

< This symbol means Is Less Than and is used like this:

If number <10 Then
MsgBox “The Number was Less Than 10″
End If

 

>= These symbols mean Is Greater Than or Equal to, and are used like this:

If number >= 10 Then
MsgBox “The Number was 10 or Greater”
End If

 

<= These symbols mean Is Less Than or Equal to, and are used like this:

If number <= 10 Then
MsgBox “The Number was 10 or Less”
End If

 

And You can combine the logical operators with the word And. Like this:

If number > 5 And number < 15 Then
MsgBox “Greater than 5 And Less than 15″
End If

 

Or You can combine the logical operators with the word Or. Like this:

If number > 5 Or number < 15 Then
MsgBox “Greater than 5 Or Less than 15″
End If

 

<> These symbols mean Is Not Equal to, and are used like this:

If number1 <> number2 Then
MsgBox “number1 is not equal to number2″
End If

 

A word about And and Or. Notice the format with And and Or. The variable is repeated twice

If VariableName = 7 Or VariableName = 10 Then MsgBox “7 or 10 spotted”

If you just put something like this

If VariableName > 7 Or < 10 Then MsgBox “7 or 10 spotted”

then Visual Basic will give you an error message. What you have to say is

If [test the variable for this value] And [test the variable for that value] Then

Your If statement can contain an Else part, too. The format is this:

If [conditional logic here] Then
Some Code here
Else
Some Other Code here
End If

But don’t worry if all that hasn’t sunk in – you’ll get used to the Conditional Operators as you go along. In the next part, there’s two little programmes for you to write. They will test the skills you have acquired so far.

VN:F [1.9.10_1130]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.10_1130]
Rating: 0 (from 0 votes)

Popularity: 100%

Add a Combo Box to a VB .NET form

Number of View: 2

Create a new project for this section. Add a button to your new form. Then, locate the Combo Box on the Visual Basic .NET toolbar. It looks like this:

The ComboBox Tool

Double click the icon to add a Combo Box to your form. Or click once with the left hand mouse button, and then draw one on the form.

A combo box is a way to limit the choices your user will have. When a black down-pointing arrow is clicked, a drop down list of items appears. The user can then select one of these options. So let’s set it up to do that.

  • Click on your Combo Box to select it. Then locate the Item property from the Properties Box:

The Items Property of the ComboBox

  • Click the grey button, as above. The one with the three dots in it. When you do, you’ll get the following box popping up:

Add an item to your ComboBox

  • To use the String Collection Editor, type an item and press Return (it’s just like a normal textbox. Each item will be one item in your drop-down box.)
  • Enter five items, as in the image below:

Five items have now been added

  • Then click the OK button at the bottom.

The Editor will close, and it will look like nothing has happened. However, run your programme and test out your new Combo Box. You should have something like this:

The Combox in Run Mode

You now need to know how to get values from the list. Once you know how to get a value from the list, you can put the value into a variable and test it with some Conditional logic.

Getting a value from a Combo Box is fairly straightforward, because it acts just like a Textbox. A Textbox has a Text property, and so does a Combo Box. To get a value from a Textbox, you would code like this

MyVariable = Textbox1.Text

Whatever is in the Textbox will be transferred to the variable called MyVariable. The process is exactly the same for a Combo Box. The code is like this:

MyVariable = Combobox1.Text

Now we are transferring whatever is selected from the Combo Box to the variable called MyVariable.

Let’s try it. Double click the button you added to your form. This will open the code window. Then enter the following code for the button:
Dim MyVariable as String

MyVariable = Combobox1.Text

MsgBox MyVariable

Run your programme. When the programme is running, select an item from your Combo Box. Then click your button. Whatever was in the Combo Box window should have ended up in the Message Box.
And that’s all there is to getting a value from a Combo Box – just access its Text Property and pass it to a variable.

Finally, the Combo Box has a DropDownStyle property. Locate this property and you’ll notice its value has a drop down box. The box contains three different Combo Box styles to choose from. Experiment with all three and see how they differ.

In the next section, we’ll take a look at Conditional Operators, what they are, and how to use them.

VN:F [1.9.10_1130]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.10_1130]
Rating: 0 (from 0 votes)

Popularity: unranked