Home and Learn: VB Net Course
Exercise A (Button code not shown)
Dim number1 As Integer
Dim number2 As Integer
Dim number3 As Integer = 10
Dim answer As Integer
number1 = 3
number2 = 5
answer = number1 + number2
answer = answer * number3
Textbox1.Text = answer
Exercise B
Dim FirstName As String
Dim LastName As String
Dim FullName As String
FirstName ="Bill"
LastName = "Gates"
FullName = LastName & " " & FirstName
Textbox1.Text = FullName
Exercise C
Dim FirstName As String
Dim MiddleName As String
Dim LastName As String
Dim FullName As String
FirstName ="Bill"
MiddleName = "Henry"
LastName = "Gates"
FullName = FirstName & " " & MiddleName & " " & LastName
Textbox1.Text = FullName
Exercise D
Dim FirstName As String
Dim LastName As String
Dim WholeName As String
FirstName = txtFirstName.Text
LastName = txtLastName.Text
WholeName = FirstName & " " & LastName
txtWholeName.Text = WholeName
Exercise E
Dim ButtonContents As String
ButtonContents = Button1.Text
txtVariables.Text = ButtonContents
Exercise F
View the full code for the calculator
Exercise G
Dim agerange As Integer
agerange = Val(textbox1.Text)
Select Case agerange
Case 13 To 19
MessageBox.Show( In your Teens)
Case 20 To 29
MessageBox.Show(In your Twenties)
Case 30 To 39
MessageBox.Show(In Thirties )
Case Else
MessageBox.Show(Not at a teen and over 39)
End Select
Section 3, Part One
Dim number_in_textbox As Integer
number_in_textbox = Val(TextBox1.Text)
If number_in_textbox > 10 And number_in_textbox < 20 Then
MessageBox.Show("Number entered was: " & number_in_textbox)
Else
MessageBox.Show("Number entered was not between 10
and 20")
TextBox1.Text = ""
End If
Section 3, Part Two
Item One, Item Two, Item Three below can be replaced by whatever items you have in your Combo Box list.
Dim MyVariable As String
MyVariable = ComboBox1.Text
Select Case MyVariable
Case "Item One"
MessageBox.Show("You like " & MyVariable & ", I see - good choice")
Case "Item Two"
MessageBox.Show("Really? " & MyVariable & "?")
Case "Item Three"
MessageBox.Show(MyVariable & "? Now you're just being silly!")
End Select
Exercise H
Dim startNumber As Integer
Dim endNumber As Integer
Dim i As Integer
Dim answer As Integer
startNumber = Val(TextBox1.Text)
endNumber = Val(TextBox2.Text)
If startNumber = 0 Or endNumber = 0 Then
MessageBox.Show("Empty textbox")
Exit Sub
End If
For i = startNumber To endNumber
answer = answer + i
Next i
MessageBox.Show(answer)
Exercise I
Dim message As String = ""
Dim counter As Integer = 0
If CheckBox1.CheckState = CheckState.Checked Then
message = message & CheckBox1.Text & vbNewLine
counter = counter + 1
End If
If CheckBox2.CheckState = CheckState.Checked Then
message = message & CheckBox2.Text & vbNewLine
counter = counter + 1
End If
If CheckBox3.CheckState = CheckState.Checked Then
message = message & CheckBox3.Text & vbNewLine
counter = counter + 1
End If
If CheckBox4.CheckState = CheckState.Checked Then
message = message & CheckBox4.Text & vbNewLine
counter = counter + 1
End If
If CheckBox5.CheckState = CheckState.Checked Then
message = message & CheckBox5.Text & vbNewLine
counter = counter + 1
End If
MessageBox.Show("You have chosen " & vbNewLine & message)
Select Case counter
Case 0
MessageBox.Show("You obviously dont watch soaps!")
Case 1
MessageBox.Show("only 1 show watched")
Case 2
MessageBox.Show("2 shows watched")
Case 3
MessageBox.Show("3 shows watched")
Case 4
MessageBox.Show("4 shows watched")
Case 5
MessageBox.Show("a soap addict")
End Select
Exercise J
Tricky one, this. The solution is to change this line:
letter = strText.Substring(1)
to this:
letter = strText.Substring(i)
By using the loop variable i you advanced the counter for Substring.
Exercise K
Dim OneCharacter As Char
Dim FirstName As String
Dim i As Integer
Dim TextLength As Integer
FirstName = txtChars.Text
TextLength = FirstName.Length
For i = 0 To TextLength - 1
OneCharacter = FirstName.Chars(i)
If IsNumeric(OneCharacter) Then
MessageBox.Show("Number Found - Exiting")
Exit Sub
End If
Next
Exercise L
Dim OneCharacter As Char
Dim FirstName As String
Dim i As Integer
Dim TextLength As Integer
Dim numbercount As Integer = 0
FirstName = txtChars.Text
TextLength = FirstName.Length
For i = 0 To TextLength - 1
OneCharacter = FirstName.Chars(i)
If IsNumeric(OneCharacter) Then
numbercount = numbercount + 1
End If
Next
MessageBox.Show("found " & numbercount & " numbers")
Exercise M
Checking for a valid email address is quite tricky. In the code below, the Sub is just checking if the email address has an @ sign. Notice that the Sub uses two paramaters, one to pass in the email address and one to pass in the @ sign. You can then reuse this code to pass in, say, an email address and the characters ".com"
'===================================
' BUTTON CODE
'===================================
Dim email As String
Dim chars_to_check As Char
email = txtEmail.Text
chars_to_check = "@"
TestEmail1(email, chars_to_check)
'===================================
' SUBROUTINE
'===================================
Private Sub TestEmail1( eMail As String, CheckChars As String)
Dim position As Integer
position = InStr(eMail, CheckChars)
If position = 0 Then
MessageBox.Show("Not a Valid email address")
End If
End Sub
Exercise N
Private Sub TextBox1_Leave( sender As Object, e As EventArgs) Handles TextBox1.Leave
Dim CheckTextBox As String
CheckTextBox = Trim(TextBox1.Text)
CheckTextBox = StrConv(CheckTextBox, VbStrConv.ProperCase)
TextBox1.Text = CheckTextBox
End Sub
Exercise O
Here are the Set and Get properties that change the height and width. (As was mentioned in the text, however, the name of the Class (ConvertPostcode) is not a very good name for what this does - changing the height and width of an image! Plus, picture boxes already have a height and width property)
Private intHeight As Integer
Private intWidth As Integer
Public Property ChangeHeight() As Integer
Get
Return intHeight
End Get
Set( HeightValue As Integer)
intHeight = HeightValue
End Set
End Property
Public Property ChangeWidth() As Integer
Get
Return intWidth
End Get
Set( WidthValue As Integer)
intWidth = WidthValue
End Set
End Property
And here is the code for the button that uses the two properties:
Dim objAlterPicPox As ConvertPostcode
Dim NewHeight As Integer
Dim NewWidth As Integer
objAlterPicPox = New ConvertPostcode
objAlterPicPox.ChangeHeight = Val(txtHeight.Text)
NewHeight = objAlterPicPox.ChangeHeight
PictureBox1.Height = NewHeight
objAlterPicPox.ChangeWidth = Val(txtWidth.Text)
NewWidth = objAlterPicPox.ChangeWidth
PictureBox1.Width = NewWidth
objAlterPicPox = Nothing
Back to the VB NET Contents Page
Email us: enquiry at homeandlearn.co.uk