2-4 Steps in Developing Application:: Visual Basic
2-4 Steps in Developing Application:: Visual Basic
Example 2-1: Design a form with one text box and two Commands
button. Write a code so when run project and click on command1 (O.k.)
replace the word (Welcome) in text box, and when click on Command2
(Close) terminate the program and return back to the form interface.
Solution:
Creating the Interface:
Setting Properties:
The next step is to set properties for the objects. The properties
window provides an easy way to set properties for all objects on a form.
For the Example )1( , you’ll need to change three property setting. Use the
default setting for all other properties.
Writing Code:
The code editor window is where you write Visual Basic code for your
application. Code consists of language statements, constants, and
declarations. To open the code window, double-click the form or control for
which you choose to write code, or from the Project Explorer window,
select the name of a form and choose the View code button.
In the Object list box, select the name of an object in the active
form. Or double click of an object.
In the procedure list box, select the name of an event for the
selected object. The Click procedure is the default procedure for a
command button and the Load is default procedure for a form.
Choose the command2 and type the following code: Private Sub
Command2_click ( )
End
End Sub
Choosing save project from the file menu. Visual Basic will prompt you
separately to save the form and then the project.
Example 2-2: Design a form shown in figure below, with three text
boxes and one Command Button. Write code in the Command1 (Execute).
Solution :
Creating the Interface:
1. Adding a Label to the form1. Double-click the Label’s Label to create a
Label with sizing handles in the center of the form1.
2. Repeat step 1 to add Label2 and Label3.
3. Adding a TextBox to the form1. Double-click the toolbox’s textbox to
create a text box with sizing handles in the center of the form1.
4. Repeat step 3 to add Text2 and Text3.
5. Adding a Command Button1 to the form. Click on button and draw
Button to form then the Button1 appears on form1.
Txt3.text=tex1.text+ “ “+txt2.text
End Sub
Running the Application:
To run the application, choose start from the run menu, or click the start
button on the toolbar , or F5 Click the command button1 (Execute) and
see the Full Name displayed in the TextBox3.
Saving a Project :
Choosing save project from the file menu. Visual Basic will prompt you
separately to save the form and then the project.
7-Long: A variable of type Long requires 4 bytes of memory and can hold
the whole numbers from -2x109 to 2x109.
3.2 Variables:
In Visual Basic, uses variable for storage values. must start with character
and maximum length 255 character and not contain any point.
The declaration means defining the variable type. The variable has to be
declared with the Dim Statement, supplying a name for the variable:
Variables declared with the Dim statement within a procedure exist only as
long as the procedure is executing. When the procedure finishes the value
of the variable disappears. In addition, the value of a variable in a
procedure is local to that procedure can’t access a variable in one
procedure from another procedure.
A variables name:
Must begin with letter.
Can’t contain an embedded period or embedded type-declaration
character.
Must not exceed 255 characters. The names of controls, forms, and
modules must not exceed 40 characters.
They can’t be the same as restricted keywords (a restricted keyword
is a word that Visual Basic uses as part of its language. This includes
predefined statements such as “If and Loop”, functions such as “Len
and Abs”, and operators such as “Or and Mod”).
The optional as type clause in the Dim statement allows you to define
the data type or object type of the variable you are declaring (see
sec.3.1).
Examples:
Dim X As Integer
Dim Balance As Currency
Dim Y As Long
Dim A AS Double, B As Double
Dim Month As Date
Dim Max As Single
After declaring various variables using the Dim statements, we can assign
values to those variables.
The general format of an assignment is:
Variable=Expression
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
Example:
firstName=Text1.Text
secondName=Text2.Text
yourName=firstName+secondName
number1=val(Text3.Text)
number2=val(Text4.Text)
number3=num1*(num2^3)
number4=number3 Mod 2
number5=number4\number1
Total=number1+number2+number3+number4+number5
Average=Total/5
4. Conditional Operators
* You can also compare strings with the above operators. However, there
are certain rules to follows:
Upper case letters are less than lowercase letters,
"A"<"B"<"C"<"D".......<"Z" and number are less than
letters.
Operator Meaning
And Both sides must be true
or One side or other must be true
Xor One side or other must be true but not both
Not Negates truth