Making Different Applications |
Creating an application in Visual Basic is simple. You have already learnt a lot about creating a VISUAL BASIC application. Now it will take just a minute to create your first VISUAL BASIC application. |
First of all let us make a Simple 'Hello' application, As we do in other programming language: |
|
Hello Visual Basic Application: |
|
|
The first step in building a Visual Basic application is to create the forms that will be the basis for your application's interface. Then you draw the objects that make up the interface on your forms. For this first application, you will use two controls from the Toolbox. |
|
1. Click tool Box for the control you choose to draw - In this case, the text box. Move the pointer onto your form. The pointer becomes a cross hair. |
2. Place the cross hair where you want the the control. |
3. Drag the cross hair until the control is the size you want. |
4. Release the mouse button. |
|
Form will look like the figure: |
|
|
|
For the "Hello, VB!" example, you will need to change three property settings. Use the default settings for all other properties: |
|
Object | Property | Setting |
Form | Caption | Hello VB |
Command Button | Caption | OK |
Textbox | Text | (Empty) |
|
|
After property setting form will look like: |
|
|
|
Writing Code: |
|
1. For this example, choose the command button, Command1. |
2. In the Procedure list box, select the name of an event for the selected object: |
|
Here, the Click procedure is already selected, because it's the default procedure for a command button. |
|
Type the following code between the Sub and End Sub statements: |
|
Text1.Text = "Hello, VB!" |
|
The event procedure will look like this: |
|
Private Sub Command1_Click () |
Text1.Text = "Hello, VB!" |
End Sub |
|
Running the Application |
|
To run the application, choose Start from the Run menu, or click the Start button on the toolbar, or press F5. Click command button you have created on the form and you will see "Hello,VB!" displayed in text box. |
|
|
|
An application using Mathematical Operations |
|
Now you will create an application which performs two operations: |
|
1. ADD |
|
2. MULTIPLY |
|
Designing the interface |
1. Select a New project from file menu,then select standard EXE. |
2. Place controls in the form to create the user interface according to the figure: |
|
|
|
3. Now for above example, you will need to change different property settings. Change the properties according to the figure: |
|
Use the default settings for all other properties. |
|
Property Table: |
|
Object | Property | Setting |
Form | Caption | Mathematical Calculations |
Command Button1 | Caption | ADD |
Command Button2 | Caption | MULTIPLY |
Command Button3 | Caption | EXIT |
Textbox1 | Text | (Empty) |
Textbox2 | Text | (Empty) |
Textbox3 | Text | (Empty) |
Label1 | Caption | 1st Number |
Label2 | Caption | 2nd Number |
Label3 | Caption | Result |
|
|
After setting the properties for different controls the form will look like figure: |
|
|
|
Coding: |
|
1. Double click on the command button ADD. |
|
Private Sub Command1_click( ) |
Dim No1 As Integer |
Dim No2 As Integer |
No1=Text1.Text |
No2=Text2.Text |
Text3.Text=No1+No2 |
End Sub |
|
Dim' statement in VISUAL BASIC is used to declare variables. |
The 1st two lines of code declare two variables named No1 and No2 , as integer. |
Then further lines of code are used to store the values entered by user in the two text boxes into the variables just declared |
The last line of code adds up the two values and displays them in textbox3. |
|
2. For multiplication of two numbers, write following code by the Clicking on Multiply button. |
|
Private Sub Command1_click( ) |
Dim No1 As Integer |
Dim No2 As Integer |
|
No1=Text1.Text |
No2=Text2.Text |
|
Text3.Text=No1*No2 |
|
End Sub |
|
3. For exiting ,type the following code to the Click event of 'Command3'. |
|
Private Sub Command3_click( ) |
End |
End Sub |
|
Now the Code Window will look like the figure: |
|
|
|
Running the Application: |
|
1. Press F5 or Select Run from menu. |
2. Now the project will run and the form will appear. Click in the text box1 and enter 10. |
3. Click in the text box2 and enter 5. |
4. Click on the button ADD,the result will be displayed in the result textbox. |
5. Click on the button MULTIPLY, the result will be displayed in the result textbox. |
|
Following figure is showing addition of two numbers: |
|
|
|
6. Click on the button EXIT to close the application. |
No comments:
Post a Comment