Sie sind auf Seite 1von 32

Working with Controls

• Controls are objects that can be inserted into the form of the VB2010
IDE for various purposes.
• You can write relevant code for them to perform certain tasks.
• Controls are categorized into Common Controls, Containers, Menus,
Toolbars, Data, Components, Printings and Dialogs. The most used
common controls are Button, Label, ComboBox, ListBox, PictureBox
and TextBox.
• Drag a button
control into the
form.
• Change its default
Text Button1 to OK
in the properties
window, the word
OK will appear on
the button in the
form.
• Next, click on
the OK button
and the code
window
appears.
• Enter the code
as in the figure
• When you run the program and click on the OK button, a dialog box
will appear and display the “WELCOME TO VISUAL BASIC 2010”
message, as shown below:
Using the Text Box
• Insert two text boxes, three labels, and one button.
• The two text boxes are for the user to enter two numbers, one label is
to display the addition operator and the other label is to display the
equal sign.
• The last label is to display the answer.
Working with Control Properties
NUMERIC DATA TYPES
Type Storage Range of Values
Byte 1 byte 0 to 255
Integer 2 bytes -32,768 to 32,767
Long 4 bytes -2,147,483,648 to -2,147,483,648
-3.402823E+38 to -1.401298E-45 for negative values
Single 4 bytes
1.401298E-45 to 3.402823E+38 for positive values
-1.79769313486232E+308 to -4.94065645841247E-324 for
negative values
Double 8 bytes
4.94065645841247E-324 to 1.79769313486232E+308 for
positive values
Variant(numeric) 16 bytes Any value as large as double
Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
+/-79,228,162,514,264,337,593,543,950,335 if no decimal
Decimal 12 bytes
+-7.9228162514264337593543950335
NON-NUMERIC DATA TYPES
Data Type Storage Range of Values
String(fixed length) Length of String 1 to 65,400 characters
String(variable-length) Length +10 bytes 0 to 2 billion characters
January 1,100 to December
Date 8 bytes
3,9999
Boolean 2 bytes True or False
Object 4 bytes Any embedded object
Varient(text) Length+22 bytes Same as variable-length string
VARIABLE NAMES
• The following are the rules when naming the variables in Visual Basic
2010
• It must be less than 255 characters
• No spacing is allowed
• It must not begin with a number
• Period is not permitted
DECLARING VARIABLES
• In Visual Basic 2010, one needs to declare the variables before using
them by assigning names and data types.
• They are normally declared in the general section of the codes’
windows using the Dim statement.

• The format is as follows:


Dim Variable Name As Data Type
ASSIGNING VALUES TO VARIABLES
• After declaring various variables using the Dim statements, we can
assign values to those variables
• The general format of an assignment is

Variable=Expression
The variable can be a declared variable or a control property value.
The expression could be a mathematical expression, a number, a string,
a Boolean value (true or false) and etc.
• firstNumber=100
• secondNumber=firstNumber-99
• userName="John Lyan"
• userpass.Text = password
• Label1.Visible = True
• Command1.Visible = false
• Label4.Caption = textbox1.Text
• ThirdNumber = Val(usernum1.Text)
• total = firstNumber + secondNumber+ThirdNumber
ARITHMETIC OPERATORS
Operator Mathematical Function Example
+ Addition 1+2=3
– Subtraction 10-4=6
^ Exponential 3^2=9
* Multiplication 5*6=30
/ Division 21/7=3
Modulus(returns the
Mod remainder of an integer 15 Mod 4=3
division)
Integer Division(discards the
\ 19/4=4
decimal places)
Dim num1, num2, difference, product, quotient As Single
num1 = TextBox1.Text
num2 = TextBox2.Text
sum=num1+num2
difference=num1-num2
product = num1 * num2
quotient=num1/num2
Label1.Text=sum
Label2.Text=difference
Label3.Text = product
Label4.Text = quotient
• BMI can be calculated using the
formula weight/( height )2,
where weight is measured in kg
and height in meter.

• Underweight = <18.5
Normal weight = 18.5-24.9
Overweight = 25-29.9
Obesity = BMI of 30 or greater
String Manipulation Using + and & signs.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim text1, text2, text3 As String
text1 = "Visual“
text2 = "Basic"
text3 = text1 + text2
Label1.Text = text3
• End Sub
Label1.Text = Len(TextBox1.Text)
CONDITIONAL OPERATORS

Operator Description
= Equal to
> Greater than
< Less than
>= Equal to or Greater than
<= Less than or Equal to
<> Not equal to
Logical Operators

Operator Description
And Both sides must be true
Or One side or other must be true
One side or other must be true
Xor
but not both
Not Negates true
If….Then Statement

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim myNumber As Integer
myNumber = TextBox1.Text
If myNumber > 100 Then
Label2.Text = " You win a lucky prize"
End If
End Sub
IF….THEN…ELSE STATEMENT
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim myNumber As Integer
myNumber = TextBox1.Text
If myNumber > 100 Then
Label2.Text = " Congratulation! You win a lucky prize"
Else
Label2.Text = " Sorry, You dif not win any prize"
End If
End Sub
IF….THEN…ELSEIF STATEMENT
Dim Mark As Integer
Dim Grade as String
Mark = TextBox1.Text
If myNumber >=80 Then
Grade="A"
ElseIf Mark>=60 and Mark<80 then
Grade="B"
ElseIf Mark>=40 and Mark<60 then
Grade="C"
Else
Grade="D"
End If
The Select Case…End Select Structure
Dim grade As String
Private Sub Compute_Click( )
grade=txtgrade.Text
Select Case grade
Case "A"
Label1.Text="High Distinction"
Case "A-"
Label1.Text="Distinction"
Case "B"
Label1.Text="Credit"
Case "C"
Label1.Text="Pass"
Case Else
Label1.Text="Fail"
End Select
The Select Case…End Select Structure
Dim mark As Single Dim mark As Single
mark = mrk.Text mark = Textbox1.Text
Select Case mark Select Case mark
Case 0 to 49
Case Is >= 85
Label1.Text = "Need to work harder"
Label1.Text= "Excellence"
Case 50 to 59
Case Is >= 70 Label1.Text = "Average" s
Label2.Text= "Good" Case 60 to 69
Case Is >= 60 Label1.Text= "Above Average"
Label3.Text = "Above Average" Case 70 to 84
Case Is >= 50 Label1.Text = "Good"
Label4.Text= "Average" Case 85 to 100
Case Else Label1.Text= "Excellence"
Label5.Text = "Need to work harder" Case Else
End Select Label1.Text= "Wrong entry, please reenter the
mark"
Looping using the For….Next Loop

For counter=startNumber to endNumber


One or more statements
Next
Do Loop
Do While condition Do
Block of statements Block of statements
Loop Loop While condition

Do Until condition Do
Block of statements Block of statements
Loop Loop Until condition
While…End While Loop

While condition
Statements
End While

Das könnte Ihnen auch gefallen