COMPUTER TRAINING: Creating Simple Application in Visual Basic

Monday, 24 December 2012

Creating Simple Application in Visual Basic

Creating Simple Application in Visual Basic
 
Structure Of A Visual Basic Application
 
VISUAL BASIC Application (Project) will be of .VBP extension and related forms are saved with .frm extension. A Simple VISUAL BASIC Project (.VBP).
 
 
Visual Basic Application (Project - saved as a file with .VBP extension) is made up of:
 
1. Forms
Windows that you create for user interface (saved as a file with a .FRM extension)
 
2.Controls
Graphical features drawn on forms to allow user interaction (text boxes, labels, scroll bars, command buttons, etc.) (Forms and Controls are also called objects.)
 
3.Properties
Every characteristic of a form or control is specified by a property. Example properties include names, captions, size, color, position, and contents.
Visual Basic applies default properties. You can change properties at design time or run time.
 
4.Methods
Built-in procedure that can be invoked to give some action to a particular object.
 
5.Event Procedures
Code related to some object. This is the code that is executed when a certain event occurs.
 
6.General Procedures
Code not related to objects. This code must be invoked by the application.
 
 
 
 
Creating Simple Application in Visual Basic
 
Designing The Visual Interface Form
 
The first step in Visual Basic application is to create the forms that will be the basis for your application' s interface .It comprises of two Steps:
 
1. Placing Controls on the form.
2. Setting Properties of Controls at Design Time.
 
Placing Controls on the form:
 
The following method will guide you about placing controls on the form.
A window will appear when you start Visual Basic.Main window will look like that:
 
 
Select the Standard EXE from the above figure.Now you will see the following figure:
 
 
As you have read earlier above window is the main VISUAL BASIC IDE ,all application are created in the IDE.The above IDE will contain six windows:
 
I. Main Window
II. Form Window
III. Tool Box
IV. Property Window
V. Form Layout Window
VI. Project Explorer Window
 
Note That:
If any of the above is not present in the IDE then you can invoke them by selecting fromview menu.
 
 
The user interface is 'drawn' in the form window.
 
There are two ways to place controls on a form:
 
1. Double-click the tool in the toolbox and it is created with a default size on the form. You can then move it or resize it.
 
2. Click the tool in the toolbox, and then move the Mouse pointer to the form window. The cursor changes to a crosshair.
Place the crosshair where you want the control at form by pressing the left mouse button and hold it down while dragging the cursor.
When you release the mouse button the control is drawn. This approach must be used to place controls in a frame or picture box control.
 
For example you have to place a textbox control to the form, follow these steps:
 
Select the text box control from the tool box by clicking on it. when selected it will appear like figure:
 
 
3.Click and drag within 'Form1' to place the TextBox at the form. 'Text box' control has a default text 'Text1'.
 
Note that:
Drag the mouse while keeping the left mouse button pressed.
 
Refer to figure:
 
 
So like 'Text Box' control, all other controls can be placed on the form. All have default names.
 
To resize a control, click the object so that it is select and sizing handles appear. Use these handles to resize the object.
 
 
Setting Properties of Controls at Design Time
 
Each control has properties assigned to it by default when you start a new project. There are two ways to display the properties of a control.
 
Þ The first way is to click on the control in the form window. Then click on the PropertiesWindow.
Þ The second way is to first click on the Properties Window. Then select the object from theObject box in the Properties Window.
 
Following figure Shows the Properties Window for a new application:
 
 
A very important property for each objects is its name. The name is used by Visual Basic to refer to a particular object in code:
 
A convention has been established for naming Visual Basic objects. This convention is to use a three-letter prefix (depending on the object) followed by a name you assign.
 
A few of the prefixes are (you will see more as you progress in course):
 
ObjectPrefixExample
FormFormfrmWatch
CommandcmdcmdExit
ButtonbtnbtnStart
LabellbllblStart
Text BoxtxttxtTime
MenumnumnuExit
Check boxchkchkChoice
Data controldatdatExample
 
 
 
 
Creating Simple Application in Visual Basic
 
Adding Code For The Program
 
The crucial step in building a Visual Basic application is to write code using the BASIClanguage. This is the most time consuming task in any Visual Basic application.
 
Code is placed in code window. At the top of code window two boxes will apllear ,the object (or control) list and the procedure list.Select an object and the corresponding event procedure. A blank procedure will appear in window where you write BASIC code.
 
After designing interface the next step is to associate the code with the controls.
 
Suppose you have to write code for commandButton.Then Ensure that you have selected the commandButton.
Double click on the commandButton.This will bring up the command window as shown in the figure below:
 
 
The 'Code Window' is where you write code for your VISUAL BASIC application.It is used to write code for various events supported by the controls. In code Window you have to select the control and event for which the code is to be written.
 
Suppose you have clicked command button .Then code window displays the following lines:
 
Private Sub Command1_Click( ) End Sub
 
Any code written between these lines will be executed when the control by the name of'Command1' is clicked upon.
VISUAL BASIC makes the writing code easier with the features that can automatically fill in properties and syntax.
For example, When you enter name of a control followed by a period in your code,the 'Auto List Members' feature presents a drop-down list of properties available for that control.
 
 
Add the following Code between above lines as shown in figure:
 
Text1.Text = "Hello Visual Basic"
 
VISUAL BASIC automatically verifies the correct syntax after you enter a line of code.It also capitalizes the first letter of command words and often adds extra spaces for readability.
 
The above written line of code simply changes the Text property of the control named Text1 to 'Hello VISUAL BASIC'.
 
Syntax: object. Property
 
At runtime above code simply displays a textbox containing Text "Hello Visual Basic", just like below:
 
 
 
 
 
Creating Simple Application in Visual Basic
 
Compiling The Project
 
Compiling is a very important process for any program. By Compiling we can check only grammatical errors (Basically syntax errors), not logical errors (concept of application).
 
For compiling a visual basic project follow Steps given below:
 
1.Go to Run Menu.
2.Select the option Start with full Compile. It will start your application with full compile,Otherwise you can start your application directly. OR You can use Key combinationCtrl+F5.
 
The figure below shows the process:
 
 
 
 
 
Creating Simple Application in Visual Basic
 
Creating An Exe
 
EXE stands for Executable. It means that run an ExE and use the application without knowing the code and other details. When you start your project like above step, it does not create an EXE.
 
Creating an EXE:
 
1. Go to File Menu.
2. Select the option projectname.exe option.
 
Note that:
 
An EXE is always created with the project name. Suppose you have given name myproject to your project, then it will create EXE named myproject.exe.
 
The following figure shows the above process:
 
 
 
 
 
Creating Simple Application in Visual Basic
 
Running The Project
 
After compiling and making EXE, now it is finally turn to run a visual Basic Application.
 
1. Go to Run menu.
2. Select the option Run.
3. Click the Start button on the title Bar OR Simply Press F5 to run your VISUAL BASICproject.
 
Following figure illustrates that:
 
 
If you have created EXE ,then simply double click on the EXE icon, like that:
 
A sample EXE icon in VISUAL BASIC:
 
 
By double clicking on the above icon you can invoke your VISUAL BASIC application.
 
 
 
 
Creating Simple Application in Visual Basic
 
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 firstVISUAL BASIC application.
First of all let us make a Simple 'Hello' application, As we do in other programming language:
 
Hello Visual Basic Application:
 
For Animated Presentation Click Here
 
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:
 
ObjectPropertySetting
FormCaptionHello VB
Command ButtonCaptionOK
TextboxText(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 thetoolbar, 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
 
For Animated Presentation Click Here
 
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:
 
ObjectPropertySetting
FormCaptionMathematical Calculations
Command Button1CaptionADD
Command Button2CaptionMULTIPLY
Command Button3CaptionEXIT
Textbox1Text(Empty)
Textbox2Text(Empty)
Textbox3Text(Empty)
Label1Caption1st Number
Label2Caption2nd Number
Label3CaptionResult
 
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