Download presentation
Presentation is loading. Please wait.
Published byStephany Richardson Modified over 8 years ago
1
Visual Basic 2005
2
Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised to 0. We could also assign a value to x after it has been declared; Dim x as Integer x = 4 Or x = val(txtnumber.text)
3
Other examples of declarations Dim price as Double = 2.05 Dim address as String = “dublin” Dim foundflag as Boolean = False Again these variables can be assigned a value after they have been initialised;
4
Dim price as Double price = 2.05 Or if taking in the price from a textbox price = val(txtPrice.text) Dim address as String address = “dublin” Or if taking in the price from a textbox address = txtAddress.text
5
Dim foundflag as Boolean foundflag = False Remember how variables are stored: x address price foundflag 4 “dublin” 2.05 False
6
Retrieving information from the user There are many ways of taking information from the user in VB. The first two we will look at are textboxes and inputboxes Taking a string from a text box or inputbox
7
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim address As String 'assign the value in the textbox to the string address address = txtAddress.Text 'Display the users address lbldisplay.Text = "You live in " & address End Sub The code
8
Getting a number from a textbox Remember that what is entered into a textbox or inputbox is text, i.e. a String. This means we have to convert it to a number for us to use. So in this case because we will be taking the number and changing it to an Integer we will only need to declare an Integer for the value entered into the textbox Dim num As Integer 'change the value in the text box to a number and assign it to the ‘integer num num = Val(txtnumber.Text)
9
The code Private Sub btnSquared_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquared.Click 'declare variables Dim num As Integer Dim square As Integer 'change the value in the text box to a number 'and assign it to the integer num num = Val(txtnumber.Text) 'calculate the number squared square = num * num 'display the result lbldisplay.Text = num & " squared is " & square End Sub
10
If statements ‘if statement to check if a grade is >= 50 and therefore a pass If grade >= 50 then lbldisplay.text = “You passed” End If ‘if statement to check if a grade is < 50 and therefore a fail If grade < 50 then lbldisplay.text = “You failed” End If
11
If Else In the last example we had to if statements, one to check if the grade was >= 50 and one to check if the grade is < 50. We can make this code more efficient by changing it to an if else statement. With an if else statement, if it is not the first condition the body of the else is executed. This makes sense as in our previous example the grade is either >= 50 or it is not. If grade >= 50 then lbldisplay.text = “You passed” Else lbldisplay.text = “You failed” End if This code is far more efficient because if the condition is true i.e. grade >= 50 then it does not check the else part. If the first condition is not true then the body of the else is executed and no condition needs to be checked. However with the last example using the two if statements both statements are checked even if the condition in the first statement is true, i.e. grade >= 50, which means that the second condition cannot be true.
12
If Elseif ‘check if grade is a Distinction, Merit or Pass or Fail If grade >= 80 then lbldisplay.text = “Distinction” Elseif grade >=65 then lbldisplay.text = “Merit” Elseif grade >= 50 then lbldisplay.text = “Pass” Else lbldisplay.text = “Fail” End if An if Elseif statement has many conditions and is more efficient than writing multiple if statements as it does not check anymore conditions if it finds a condition that is true, e.g. in the above example if the grade was 67 then the second condition is true and “Merit” is printed and no more conditions need to be checked.
13
Validating a Textbox When taking a users input with a textbox we need to validate the textbox to ensure the correct information has been entered. The first thing to check for is that the textbox is not empty. If it is empty then we must alert the user by means of a Messagebox and put the cursor in the textbox for the user. ‘check for the empty string, i.e. is the textbox empty If txtgrade.text = “” then ‘prompt the user with a Messagebox Messagebox.show(“Please enter a grade”, “Error”) ‘put the cursor in the textbox txtgrade.Select() ‘Don’t continue with the rest of the code Exit Sub End if
14
In this case when we are checking for numbers being entered as we are taking in a grade. So to check that only numbers are being entered we can use the following if statement. ‘check for numbers only in the textbox If Not IsNumeric (txtgrade.text) then ‘prompt the user with a MessageBox Messagebox.show(“Please enter numbers only”, “Error”) ‘put the cursor in the textbox txtgrade.Select() ‘Don’t continue with the rest of the code Exit Sub End if
15
And If we needed to check that the number is within a certain range, e.g. 1 – 100 then we can use an if statement as follows grade = val(txtgrade.text) If grade > 0 AND grade < 101 then lbldisplay.text = “You entered a valid grade” Else lbldisplay.text = “That grade is not valid” txtgrade.text = “” txtgrade.select Exit Sub End if
16
To check that only numbers are entered we would add the following, grade = val(txtgrade.text) If Not IsNumeric (txtgrade.text) then lbldisplay.text = “Enter numbers only” txtgrade.text = “” txtgrade.select() Exit sub ElseIf grade > 0 and grade < 101 then lbldisplay.text = “You entered a valid grade” Else lbldisplay.text = “That grade is not valid” txtgrade.text = “” txtgrade.select Exit Sub End if
17
OR If we needed to check if one grade or the other was greater than 50 we would use OR Dim grade1 as Integer Dim grade2 as Integer grade1 = val(txtgrade1.text) grade2= val(txtgrade2.text) If grade1 > 50 OR grade2 > 50 then lbldisplay.text = “At least one grade greater than 50” Else lbldisplay.text = “No grades greater than 50” End if
18
Checking for letters only If we need to check that no digits have been entered we can use the following if statement Address = txtaddress.text if IsNumeric(txtaddress.text) then MessageBox.show(“Enter non digits only”, “Error”) txtaddress.text = “” txtaddress.select() Exit sub Else lbldisplay.text = “You live in “ & address End if
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.