Images

Visual Basic - PART II


Visual Basic
Visual Basic is a high level programming language evolved from the earlier DOS version called BASIC. [Beginners' All-purpose Symbolic Instruction Code], programming language from Microsoft specialized for developing Windows applications.VB
Visual Basic is Easy to learn Programming language. WithVisual Basic you can develop Windows based applications and games. When first released in 1991, it was similar toMicrosoft's QuickBASIC, With its visual user interface development tools.
Visual Basic became very popular by the mid-1990s. Visual Basic is much more easier to learn than other language (like Visual C++), and yet it's powerful programming language.
In VISUAL BASIC, programming is done in a graphical environment. Because users may click on a certain object randomly, so each object has to be programmed independently to be able to response to those actions (events).


Methodes and Events
Methods
Methods are associated with objects, and define the actions they carryout for their object.
For Example:
The methods of your radio are on, off, volume control etc, Which give you control on your radio.Each Method performs a particular action. Method is an action that an object is capable of performing.
For example:
list boxes have methods called AddItem, RemoveItem and Clear, for maintaining list contents.

Methodes and Events
Syntax for Calling Method
Objectname-Method
Here objectname specifies the name of an object, and Method specifies the name of a method.
Example:
Form1.Print "Hello World"
Here Form1 is form name , and method Print is called to print the String "Hello World" to form.
There are different methods associated with different controls.Methods are useful because they hide the details of how an action is performed.
The programmer just needs to call the method ,how the action is performed it is not his or her headache.
Note That:
Put a dot after the name of control then various options will look like in figure.
Methods associated with some commonly used controls:
List Box
It is control that provides a list of items.
Methods of List Box:
1) Additem: This method adds the specified item to the list
2) Removeitem: This method is used to delete an item from the list.
3) SetFocus: Used to make the list box the Current active element.
Combo Box:
combo box has all the properties exhibited by the text box and list box.
Methods of combo box:
1.Additem: This method will add the specified item to the combo box.
2.Remove Item: This method will delete an item from the combo box.
Option Button:
Option button are used when the user can select one and only one of the multiple options.
Typically,there are no special methods associated with option buttons.A method associated with option
button is:
Move: Moves a control on the form.
Command Button:
The easiest way to allow the user to interact with an application is to provide a button toclick.
Visual Basic provides this in form of Command Button.
One method is used with the command button:
Move: Moves a control on the form

Methodes and Events
Events
A major part of interaction between people in every day life is in the form of events andresponse.
For Example
When some one knocks at the door, then you open the door.Knocking at door is event and opening the door is response to the event.
In technical words an event is an user action directed at the application.

Methodes and Events
Example Of An Event In An Application
1.Clicking the mouse.
2.Pressing a key on the keyboard.
When you click on a button it is recognized as an event by the button.
calculator (Provided in your Windows) shown in figure is the perfect example of events and their quick responses.
For Example
Clicking on a number Button will display a particular number in the text Box.Some ofevents like methods require additional information in the form of arguments.
For Example,
Click event does not requires arguments whereas DragDrop event requires arguments as shown in example:
Public Sub Command1_Click()
Statements
End Sub
Public Sub Commnand1_DragDrop(Source As Control,X as Single,Y As Single)
Statements
End Sub

Working with Forms
Form Basics
A form is a window that contains application code and other objects placed on it to create the user interface. Forms may fill the entire screen or have other forms contained with in it.
Note that:
Visual basic initially includes a default form Form1 file in each project.You can change the name and caption of the form to identify the purpose of the form.
Every form has its own properties ,events and methods associated with it.
Here a sample form is shown, VISUAL BASIC gives default name and caption to a form asform1. It is shown in above figure.
Adjusting of height and width is shown in figure below:
Form objects are the basic building blocks of a Visual Basic application. It is actual window with which a user interacts when they run the application.
Forms have their own properties, events and methods with which you can control their appearance and behavior.

Working with Forms
Form Properties
Many properties of a form affect its physical appearance. The Caption property determines the text that is shown in the form's title bar, the Icon property sets the icon that is displayed when a form is minimized.
The MaxButton and MinButton properties determine whether the form can be maximized or minimized. By changing the BorderStyle property, you can control the resizing behavior of the form.
There are some specific properties of the form and a brief description of some selected properties is given here.
Property window of a form is shown below:
Border Style:
Form can have different types of borders.
The borders are as follows:
Caption:
The title of the window is stored in Caption property.It is shown at top of the form.
Icon:
This property specifies the icon for the window in the upper-left corner of the window.
Mouse Pointer:
This displays the value that indicates the type of mouse pointer.
Max Button:
This property indicates whether the maximize Button should be shown and Maximize choice available in the control box menu.
Min Button:
This property indicates whether the minimize Button should be shown and minimize choice available in the control box menu.
MDI Child:
This property specifies if this window must be shown within a multiple document interface(MDI) window. You will discuss them in detail in further section of tutorial.
Back Color:
Back Color is used to fill the back ground color of the form.
Autoredraw:
Setting AutoRedraw to True always produces normal layering. While using AutoRedraw is the easiest way to layer graphics, applications with large forms may suffer from reduced performance due to the memory demands of Autoredraw.
For Example:
By setting Autoredraw property of form to true ,You print any text at the form.

Working with Forms
The Form Events
As objects, forms can perform methods and respond to events.The Resize event of a form is occured whenever a form is resized, either by user interaction or through code. This allows you to perform actions such as moving or resizing controls on a form.
The Activate event occurs whenever a form becomes active form, the Deactivate eventoccurs when another form or application becomes active. These events are convenient for initializing or finalizing the form'sbehaviour.
OR For writing the code for any from use key combination Alt+V+C.
A form's events are shown below in the figure:
For Writing code of a particular event just click on a particular event ,then the following code window will appear:
Here for example you have clicked the MouseUp event.
Using the unload Event:
The unload event procedure is used to verify that the form should be unloaded.The unload event is occurred when:
1.The Form is unloaded using the Unload statement.
2.The form is closed by either clicking the Close command on the application menu or clicking the close button on application title bar.
Commonly used Form Events:
ActivateForm_Activate event is triggered when form becomes the active window.
ClickForm_Click event is triggered when user clicks on form.
DblClickForm_DblClick event is triggered when user double-clicks on form.
LoadForm_Load event occurs when form is loaded.This is a good place to initialize variables and sets any run-time property.

Working with Forms
The Form Methods
Different methods are used in forms. When you put a dot after name of the form then apopup menu is displayed.
There are different methods for forms .Some of them are describing here.
Show method
Show method is used to display the form object.
For Example to display the form frmStudents,
following code is written:
frmStudents.Show
Hide Method
The Hide method is used to hide a form.
For example to hide a form, following code is written:
frmStudents.Hide
The Cls Method
All text and graphics on the form that were created with Print and graphics methods can be deleted with the Cls method.
Example: frmStudents.Cls
The Refresh Method
Forces a complete repaint of a form.
For example:
form1.Refresh
Use the Refresh method when you want to Completely display one form, while anotherform loads.
Point Method
Returns the red-green-blue (RGB) color of the specified point on a Form. It returns color as a long integer.
form1.Point(x, y)

Working with Forms
Setting Runtime & Design Time Properties
Visual Basic operates in three modes.
1. Design mode - used to build application.
2. Run mode - used to run the application.
3. Break mode - used for debugging application.
Many properties can be set at either design time or run time. At design time controls are added to the form, their properties are set and code is written. During runtime you examine your program in action, just as the end user will see it.
To set a property at design time , following steps are followed:
1. Select the form or object whose characteristics are to be changed.
2. Activate the properties window.
3. Scroll the property you want to set and select its name.
4. Enter a new setting.
At runtime, property can be changed by a program code.
This is done like this:
For Animated Presentation Click Here
when you run the above application then the form1's backcolor, mouse pointer and Caption will look like figure.

Working with Forms
Setting Start Up Form
The first form in your application is designated as the startup form. When your application starts running, this form is displayed. User will see Startup form when application starts.
If you want a different form to display when your application starts, you must change thestartup form.
To change the startup form
1. From the Project menu, choose Project Properties.
For Animated Presentation Click Here
2. Choose General tab.
3. In the Startup Object list box, select the form you want as the new startup form.
4. Choose OK.
The following figure will describe the above procedure:
In above figure you can choose the start up form from1 or form2. If you opt for form2then after executing the application form2 will be shown.

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.

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.

Working with Files
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:

Working with Files
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.

Working with Files
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:

Working with Files
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.

Working with Files
Exiting From Visual Basic
For existing Visual Basic IDE, you Simply Go to file Menu and Click Exit. Menu will look like below:
You can also use Alt+Q for Exit.

0 comments: