VB.Net Tutorial

VB.Net Tutorial #3

Welcome to the third VB.Net tutorial. You'll be please to know that we'll getting into something a little meatier in this tutorial including displaying some windows (oh, alright, message boxes). Something I haven't mentioned is indenting of the code with tabs. By indenting blocks of code, it makes it much easier to read and to make sense of all the various blocks of code. Right, now we get into making decisions using the if statement. It looks soemthing like this:-

Imports System

Class hello

	Public Shared Sub Main()
		Dim i As Integer = 1
		Dim j As Integer = 2
		If i = j Then
			Console.WriteLine("First part of statement 1 is executed")
		Else
			Console.WriteLine("Second part of statement 1 is executed")
		End If
		If j > i Then
			Console.WriteLine("First part of statement 2 is executed")
		Else
			Console.WriteLine("Second part of statement 2 is executed")
		End If
	End Sub
End Class 

The if statements breaks down into the following set of statements:-

if something
do something
otherwise
do something else

which in VB.Net terms is:-

if something
do something
else
do something else
end if

So in the top of the program we assign variables as before. Then there is a block of code like this:-

		If i = j Then
			Console.WriteLine("First part of statement 1 is executed")
		Else
			Console.WriteLine("Second part of statement 1 is executed")
		End If
		

The first section is asking if variable i is equal to j then the first statement is executed and the else part is ignored. If i is not equal to j then the else comes into play and the second statement is executed and displays a different message.

The second block of if code has a different comparison in the brackets as follows:-

If j > i Then

This is saying if j is greater than i (the sharp right bracket) then statement 1 is executed otherwise statement 2. Other comparison operators are:-

< less than
<= less than or equal to
>= greater than or equal to

Have a play around with the code. Try these comparisons and switch them around and see if they react the way you expect.

Now we come around to displaying some message boxes. If you are familiar with Visual Basic 6.0 then you will have come across them. They were called msgbox in VB6 but this has been changed to MessageBox.show. Here's some code to display one:-

Imports System
Imports System.Windows.Forms

Class hello

	Public Shared Sub Main()
		MessageBox.Show("Hello")
		MessageBox.Show("Hello", "My Hello Application", MessageBoxButtons.OKCancel, _
			MessageBoxIcon.Exclamation)
		If MessageBox.Show("Yes or No?", "My Hello Application", _
				MessageBoxButtons.YesNo, MessageBoxIcon.Question, _ 
				MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
			Console.WriteLine("You selected Yes button")
		Else
			Console.WriteLine("You selected No button")
		End If
	End Sub
End Class 

Note that long lines can be joined together using an undescore character. The program starts with another imports statement which includes the windows forms library which we need even though we are still programming in a console window. The first block of code just displays a messagebox with its default look. The second block displays it with a title as well as an instruction to the user and displays 2 buttons (OK and Cancel) and an exclamation mark icon. There are other icons you can display (question, asterisk etc) and other types of button layout and these will popup time and time again during the course of these tutorials. The next lot of code has the by now familiar if statement which displays a messagebox this time with Yes/No buttons and a question icon. It also sets the default button to be the first button (the second would be button2). This rather convoluted statement is also looking for the user to click on the Yes button and displays the appropriate message in the console window - if No is selected then the else statement comes into play. It may look complicated but it is just a series of staements seperated by commas and it can be as simple or as complicated as you like. It breaksdown as follows:-

Show the nessagebox with a message to the user (Yes or No?), a title of the box ("My Hello Application), Yes/No buttons, display Question icon and set the default button (the one surrounded by a black line and the one that will respond to a keypress such as SPACE or RETURN) to the first button taken from right to left. Nothing to it really. The only other thing to know about messageboxes is that they are modal (they stop the program and other messageboxes from running) and always display in the centre of the screen.

Well that was nice and easy. Now how about showing an InputBox (asks the user to input a string). Piece of cake in VB (not so easy in C#) as follows:-

Imports System
Imports Microsoft.VisualBasic
Imports System.Windows.Forms

Class InpBox

	Public Shared Sub Main()
		Dim message As String
		Dim title As String
		Dim defaultValue As String
		Dim Result As String

		message = "Enter a name"
		title = "InputBox Demo"
		defaultValue = "dave"

		Result = InputBox(message, title, defaultValue, 100, 100)
		If Result.ToLower = "nick" Then
			MessageBox.Show("Hello " + Result)
		Else
			MessageBox.Show("Incorrect name " + Result + ". Should be nick")
		End If
	End Sub
End Class 

So what is all this doing? In Main() we setup some variables. Then they are all assigned a value. Not only does the InputBox have a title and a user message but has a default value that is put into the text box which the user either accepts or overtypes. It can also be placed on the screen usibng the 100,100. All of these MUST be included in the call to the InputBox routine otherwise a compiler error is generated. Then there is the result (which is changed to lower case) and the rest you should be able to work out for yourself.

I think that wraps it up for this tutorial. If you have any problems or queries then please email me at the address below. On to loops and things next time. See you there...

Back to JScript.Net Tutor

Written by Nick Cheesman. Last updated: 01/06/2007
Please eMail me at:
nickjc@nickjc.co.uk