Visual Basic (part 4)


Creating Simple Application in Visual Basic

Structure Of A Visual Basic Application
A VISUAL BASIC Application (Project) will be of .VBP extension and
related forms are saved with .frm extension. A Simple VISUAL BASIC
Project (.VBP).



A Visual Basic Application (Project - saved as a file with .VBPextension) 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.


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 from view 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 Properties Window.
Þ The second way is to first click on the Properties Window. Then select the object from the Object 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):

Object Prefix Example
Form Form frmWatch
Command cmd cmdExit
Button btn btnStart
Label lbl lblStart
Text Box txt txtTime
Menu mnu mnuExit
Check box chk chkChoice
Data control dat datExample


Adding Code For The Program
The crucial step in building a Visual Basic application is to write code using the BASIC language. 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:



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 combination Ctrl+F5.

The figure below shows the process:



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:



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 BASIC project.

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.


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.


Working with Files

Saving Project or Files
Now you have created application, so you have to save the application.

Steps:

1. Go to File Menu.
2. Select Save project option .
3. Select Save project As. , if you want to save the project under a different name.
VISUAL BASIC project will be saved with the extension .vbp.
4. Now select Save Formname option.
A form will be saved with the extension .frm.
5. Click on Save button on the title bar OR
You can use Key Combination Ctrl+S for Saving Project and forms.

The window look like below:


For saving a form file, window will look like:


Note that:
If you want to change the name of your project then:

1. Go to project menu.
2. Select Project Properties.

The window will appear like below:


Now change the name of the project and click OK.

Important:

It is important to be noted here that project name and project file name are different.
Same is true in case of forms. Form name and form file names are different.


Opening Existing Project
Suppose you have to open a existing project, then follow the steps below:

1. Go to the File Menu.
2. Click on to the open project project.
3. Click on the Open button on the title Bar OR You can use Ctrl+O for opening a existing project.

The window looks like below:



Adding a File To The Project
You can add a file form different project in your project. You can share files between projects.
If you add a file to a project, you are simply including a reference to the existing file in the project. you are not adding a copy of the file to the project.

Therefore, if you make changes to the file and save it, your changes will affect any project that includes the file.

Steps for adding files:

1. Go to project menu.
2. Select Add File option OR Use Ctrl+D for adding file.

The following Dialog box will appear:


Select required file and Click Open.


Removing File From The Project
If you want to remove a form file from an existing project than:

1. Go to project Explorer Window.
2. Select form which you want to remove.
3. Right click on that. A pop menu will appear like that:



Removing Project
For removing the entire project two methods can be used:

1. Go to the file menu than select Remove Project.
2. Go to the project Explorer Window.
3. Select the project which you want to remove.
4. Right click on that. A pop menu willappear like that:


You can also use Atl+F+R for removing the entire project.

==============> Course Complete <==============

No comments:

Post a Comment