Sie sind auf Seite 1von 28

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

~ ~ ~ INDEX ~ ~ ~
TOPIC INTRODUCTION of VISUAL BASIC INTEGRATED DEVELOPMENT ENVIRONMENT ...... STANDARD TOOLBAR ................................................. TOOL BOX ..................................................................... CREATING PROJECTS . MODES in VISUAL BASIC LABEL, COMMANDBUTTONS and TEXTBOX ... VARIABLE, DATATYPES .. OPERATORS .. CONTROL STRUCTURE ... LOOPING STATEMENT ARRAYS .. PROCEDURES .. FUNCTIONS .. MENU and MENU EDITOR .. MDI and SDI .. LISTBOX and COMBOBOX .. IMAGEBOX and PICTUREBOX ... FRAME, OPTIONBUTTON and CHECKBOX .. TIMER CONTROL . SCROLL BAR . ADODC .. RECORDSET PROPERTIES and METHODS ... DRIVELIST, DIRLIST and FILELISTBOX .... SHAPE and LINE CONTROLS ...... OLE CONTROLS ....................... .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. .................. PAGE 02 02 ~ 03 04 05 ~ 06 06 06 07 ~ 08 09 ~ 10 10 ~ 11 11 ~ 13 13 ~ 15 15 ~ 16 16 16 ~ 17 17 ~ 19 19 ~ 20 21 ~ 22 22 22 ~ 23 24 ~ 26 26 26 ~ 27 27 27 27 ~ 28 28

Note: If you find any typing or printing error/s, inform immediately to your branch office.
ADCPM

VISUAL BASIC

Page - 1

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

Introduction of VISUAL BASIC: Visual Basic6.0 is a powerful programming


system that helps one to develop sophisticated, graphical applications. It is the modified version of BASIC (Beginners All purpose Symbolic Instruction Codes) Language (1960). It is in use since 1998. It is also called as Rapid Applications Development system, is used to build applications quickly. These applications can be run on Microsoft Windows environment. The original Visual Basic for DOS and Windows were introduced in 1991. Visual Basic 3.0 (a vast improvement over previous versions) was released in 1993. Visual Basic 4.0 released in late 1995 (added 32 bit application support). Visual Basic 5.0 released in late 1996. New environment, supported creation of ActiveX controls, deleted 16 bit application support. Features of Visual Basic: Visual programming environment provides all features that are required to develop a graphical user interface, as ready to use components. 1. Visual Basic provides a Common Programming Platform. 2. Visual Basic support Event Driven Programming. 3. Visual Basic offers many Useful Tools for Programming. 4. Visual Basic provides many Supporting Wizards. 5. Visual Basic provides ActiveX Support for Data Control. 6. Visual Basic provides Quick Error Detection or Correction. 7. Visual Basic is a Faster Compiler. 8. VB allows Database Integration with Wide Variety of applications. 9. Visual Basic can be used as a New Data Report Designer. 10. VB has some Additional Internet Capabilities.

What is IDE?
The working environment in Visual Basic6.0 is integrated with many different functions. Such as: Designing, Editing, Compiling, Debugging with in a common environment. For this reason, the working environment of Visual Basic is also referred to as the Integrated Development Environment (IDE).

COMPONENTS: 1. Form Window: 2. 3. 4. 5. 6.


Page - 2

In form window you can design a form by the different controls are placed on the form. Project Window: In the project explorer, window shows the list of form and modules in a project. Property Window: The property window lists the properties of a selected control or for the form as it caption, sizes, color, etc. Form Layout Window: The form layout window shows how big a form is in relation to the screen. Tool Bar: A tool bar is a bar that displays icons for commonly used task. Tool Box: A tool box is a window that displays a set of tools that may be used to places controls on a form.
VISUAL BASIC
ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

ADCPM

VISUAL BASIC

Page - 3

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

Standard Toolbar: Standard toolbar contains some commonly used buttons. Icon Name Description Standard EXE project To add the Standard EXE project. ActiveX EXE project ActiveX DLL project ActiveX control Add Form MDI Form Module Class Module User Control Property Page Menu Editor Open Project Save Project group Cut Copy Paste Find Undo Delete Redo Delete Start Break End Project Explorer Properties Window Form Layout Window Object Browser Toolbox Data View Visual Component Manager
Page - 4

To add the ActiveX EXE project. To add the ActiveX DLL project. To add the ActiveX control/User control. To add the new form in your project. To add MDI form in your project. To open the module code. To open the Class Module. To open the User Control. To open the Property page. To open the Menu Editor dialog box. To open the Open Project dialog box. To open the Save File As dialog box to save the project and forms. To cut the control from the form. To create a copy of the control in the form. To paste the copied control in the form. To open the Find Dialog box. To repeat the last delete control. To repeat the last Undo delete. To start compiling/run the form. To break the form compiling. To end/stop the form compiling. To open the Project Explorer. To open the Properties Window of the controls. To open the Form Layout Window. To open the Object Browser. To open the Toolbox. To open the Visual Component Manager.
VISUAL BASIC
ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Visual Basic Tool Box: The Visual Basic toolbox contains the tools that allow you to draw controls on your forms. Tool Name Pointer Control Picture Box Label Text Box Frame Command Button Check Box Option Button or Radio Button List Box Description This tool lets you select, resize or move a control. This control is used to display graphical images. A label control is a graphical tool that used to display text that a user can not change directly. A text box is a control that displays information that you entered during design mode. A frame control is used to separate different groups of controls on form. A command button control is used to begin, interrupt or end a process. It is used to allow the user select multiple choices. An option button is used to display an option that can be turned on or off. Option button is used for a group of option wherefrom user can select just one.

A list box control displays a list of items from which the user can select one or more items. Combo Box A combo box combines the features of a Text box and a List box control. Horizontal & The Horizontal & Vertical scroll bar controls display a long stripe with an indicator that lets the user select a value Vertical between the two ends of a control. Scroll Bars Timer It is an invisible control which is added to a form if some task is to be repeated at regular intervals. Drive List Box This control displays the names of the drives available on a PC. Directory List It lists the folder in the current drive, the drive mentioned in its Path property. box File List Box This control locates and lists files in the directory whose location or path has been specified in the Path property of the File list box. Shape The shape control is a graphical control that is used to display a rectangle, circle, square, oval etc. Line This control is a graphical control that is used to display a horizontal, vertical or diagonal line. Data This control is used to perform data access operations.
ADCPM

VISUAL BASIC

Page - 5

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

Image OLE

This control is used to display a graphic. This control enables you to add insertable objects to your forms.

Creating an Application (New Project):


The following steps are followed by the user for opining a new project open: 1. Click at start button. 2. Click at Program and then Microsoft Visual Studio and click on Microsoft Visual Basic. 3. After clicking Microsoft Visual Basic, a wizard will be open. Here you can select type of new Project.

A project in Visual Basic is a collection of several different types of file that make a pure program. Various types of program that you can create here: Standard EXE project. ActiveX EXE/DLL ActiveX Control etc. Visual Basic Application Modes: Visual Basic application works in three modes: 1. Design mode: When a new application is being created or designed, it is in design mode. 2. Run mode: when the application is executed, it is said to be in run mode. 3. Break mode: while an application is in a state of suspension, it is said to be in break mode.
Page - 6

VISUAL BASIC

ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Adding Controls to a Form: Visual Basic provides a tool box contains the
tools that you can used to draw controls on your form. When you want to add control in your form, first click on the control and then go to form and click where you want to draw control by pressing left mouse button drag the control. Writing Event-Driven Code: Codes in a Visual Basic application is divided into smaller blocks are called procedures. Control event procedure is a procedure contains codes that executed when an event occurs. To create an event procedures double click the object for which event procedure to be written. It will invoke code editor window where you can write the code for an event associated with the selected control.

Running an Application:
When you want to run a program, then you have to follow these steps: 1. After creating the code in the code window, then select start option in run menu through menu bar or use F5 key. 2. Now your program in the run mode and you can put the data and show the result. Now you can run any application created with Visual Basic.

WORKING with LABEL, COMMANDBUTTONS and TEXTBOX: LABEL Control:


The Label control displays text that the user cannot directly change. Basically labels are used to identify controls that do not have their own Caption property. The actual text displayed in a label is controlled by its Caption property. Property Caption Font Description To display the text in the Label control. To specify the display font, style and size for the Caption of the Label. Font is selected by clicking at () button after clicking in Font property box. Name To name the label control. VB identifies a label through the text given under this property. Alignment To specify the alignment of labels caption. 0 : left Justify 1 : Right Justify 2 : Center WordWrap This property is used to set the word wrapping option for the caption text. By default if the text exceeds the width of the Label, the text wraps to the next line. However, if the text goes beyond the height of Label then it is clipped. Autosize Causes the control to horizontally expand and adjust to the size of its contents. BorderStyle If this property set to 1, the label appears with a border- giving it a look similar to a text box.

TextBox Control:
Text boxes are versatile controls that can be used to get input from the user or to display text. Basically the Text Box control is a small text editor that provides all
ADCPM

VISUAL BASIC

Page - 7

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

the basic text- editing facilities. That are, You can insert and select text You can scroll the text if it does not fit in its visible area. You can even exchange text with other application through the clipboard. Property Name Text MaxLength MultiLine Description To specify the name To specify text to be displayed in the textbox. To set the maximum number of characters allowed in the textbox. If it set to true, allows multiple lines of text in the TextBox. To attach a built in scroll bar to the textbox. 0 : none 1 : horizontal 2 : vertical 3 : both To set any special character, which you want to display at the place of input password. Property that returns the selected text.

Scrollbar

Password SelText

Command Button:
The command button ActiveX controls allows users to click it to perform actions. When a user chooses the button, it not only carries out the appropriate action, it also looks as if its being pushed in and released. Whenever the user clicks a button, the click event procedure is invoked. You can place code in the Click event procedure to perform any action you choose. Property BackColor Description This property specifies the command button background color. Before setting the background color for command button you must change the style property from 0-Standard to 1-Graphical. This property determines whether the command button gets a Cancel Click event if the user presses Esc. Caption To hold the text that appears on the command button. Font Produces a font dialog box in which you can set the caption font name, style, and size. MousePointer This property determines the shape of the mouse curser when the user moves the mouse over the command button. Height Holds the height of the command button in tips. Picture This property holds the name of an icon graphic image that appears on the command button as long as the Style property is set to 1-Graphical TollTipText To hold the text that appears as a ToolTip at runtime. Visible This property determines whether the command button appears or is hidden from the user.
Page - 8

VISUAL BASIC

ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Variable: Sometimes you need to store some values while your program is
running .For example, a program handling banking transaction might need to store the total amount deposited on a day. Since this value is going to vary from time to time, it is to be stored location whose contents can be changed or varied. This storage location is called a variable. A named storage location, whose contents can be varied, is called variable. Datatype: Visual Basic provides facilities to work with different types of data. These different types of data can be worked with through datatypes. Visual Basic provides a following datatypes: Boolean, Byte, Currency, Date, Double, Int, Long, Single, String, Variant etc Datatypes are actually means to identify the type of data and associated operations with it. Datatype Boolean Byte Currency Date Double Integer Long Single String Description This datatype can take one of the two values true or false. This datatype allows positives integers in the range 0 to -255. This datatype is used to hold currency data. This datatype is used to hold date and time. The datatype to hold numeric values. It is short for double-precision values. The datatype to hold non-fractional value (range -32768 to +32767) The datatype to hold integer values larger than those supported by integer datatype. It is short name for Long Integer. It allows upto 6 digits of precision. Datatype to hold multiple characters textual data. It can store data having digits, alphabets and other characters.

Declaring Variables: Declaring variable means telling the program about it, in advance. A variable can be declared as per following syntax: Syntax: Dim<varname>[As<datatype>],[<varname>[As<datatype>]] Where Dim is the keyword that tells VB that a variable is being declared. <varname> is the variable name. As is another keyword that tells VB the datatype of the variable. And the <datatype> is a legal datatype in VB. [ ] brackets mean the part in it is optional. Example: Dim rollno as Byte.

Variable Scope: When you declare a variable, its not necessary that you can
use them everywhere in your application. This is because a variable may not be accessible everywhere in the application. The area of program is accessible; it is called its scope. A part of program in which a variable is accessible is known as its scope. In Visual Basic, three types of scope: 1. Private or Local Scope: If you are giving a Dim statement within a
ADCPM

VISUAL BASIC

Page - 9

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

procedure, it means that this variable will be available and accessible only within this very procedure. And thus this variable is said to be Private or Local to this procedure. A variable that can be used only in one procedure, in which it is declared, is said to have Private Scope. 2. Module Scope: Modules are a place where you can put your commonly used routines, functions, constants etc. A variable that is available in side a module that is to all these procedure in then module is said to have Module Scope. 3. Public Scope: The variable available to all the modules and procedures in an application are said to have Public Scope. An application can have many modules and procedures and they all can share Public variables Public keyword declares public variables. Since the Public variables are available publically or globally to all modules of the application, these are also known as global variables, these are available with global scope. Saving a Form: When you want to save a new form, then you can use the following steps: 1. First goto File menu and click on it, now a dropdown submenu will open. 2. Now goto save form (number) and click on it. 3. Now Save File as dialog box will open. Here you can give the name of form and click on Save Button. In this way you can save a new form. Saving a Project: When you want to save a new project, then you can use the following steps: 1. First go to File menu and click on it, now a dropdown menu will open. 2. Now go to save Project (number) and click on it. 3. Now Save Project as dialog box will open. Here you can give the name of Project and click on Save Button. In this way you can save a new Project.

PROGRAMMING BASICS
OPERATORS in VISUAL BASIC:
Operators are the symbol for word that trigger an action on some data, the data on which operator operate are called operands. Operators can be divided into following categories:

1. Arithmetic Operator
Operator + * / \
Page - 10

Operation For addition Subtraction Multiplication Division int division

Example 12 + 5.5 =17.5 12 - 5 =7 12 * 5 =60 7 / 2 =3.5 7 \ 2 =3


ADCPM

VISUAL BASIC

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Mod ^

Modulus Exponentiation

7 Mod 2 =1 7 ^ 2 =49

2. Relational Operator Operator


= <> > < >= <=

Function

Example
Val1=Val2 is false Val1< >Val2 is true Val1>Val2 is true Val1<Val2 is false Val1>=Val2 is true Val1<=Val2 is false

Equal Not Equal Greater than Less than Greater than or Equal to Less than or Equal to

3. Logical Operator Operator Function


AND

Example: (Val1=300,Val2=500)

OR NOT

XOR

If both the operands Val1>=300 AND Val1<Val2 returns conditions are true, it true since both the operand conditions returns true otherwise false. Val1>=300 and Val1<Val2=500 are true Returns true if at least one of the operand condition is true otherwise false. Negates the results of a NOT (Val1=300) is false as Val1=300 condition is true and NOT true is false. Returns true, if one operand Val1=300 XOR Val2=600 is true condition is true and other is false. It returns false, if both operand conditions are true or false.

CONTROL STRUCTURE:
Visual Basic provides such tool by providing statement to attend so. Such statement are called program statement every programming language provides control to support sequence selection or iteration. Ifthen Statement: If then statement test a particular condition if the condition evaluate to true, a course-of-action is followed otherwise the course-of -action is ignore. Syntax: If (Boolean exp) Then Statement End If Example: Write a program that tests whether a number typed in text box txtNum is positive (>0) or not. Solution: Dim Num As integer Num =Val(txtNum.Txt) If Num>0 Then
ADCPM

VISUAL BASIC

Page - 11

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

MsgBox(number is positive) End If If-Then-Else Statement: If the expression evaluates to true the statements between, If and Else are executed, otherwise, the statements between Else and End If are executed. Syntax: If(Boolean Expression) Then Statement Else Statement End If Example: WAP to input a number and check the number is Positive or Negative. Solution: Private Sub Command1_Click() Dim num As Integer num =Val(Text1) If num > 0 Then MsgBox (the number is Positive) Else MsgBox (the number is Negative) End If End Sub SelectCase Statement: VB supports a statement, called Select Case statement that handle such multiple choice condition better than if else the select case statement can be use when multiple if statement become difficult to read. Syntax: Select Case expression Statement End Select Example: Obtain a character from uses and check whether it is an uppercase character or a lowercase character or a digit or any other character. Solution: Ch =InputBox(Enter a character) Select Case Asc(ch) Case Asc (A) to Asc (Z) MsgBox it is an uppercase character Case Asc (a) to Asc (z) MsgBox it is a lowercase character Case Asc (0) to Asc (9) MsgBox it is a digit Case Else MsgBox it is any other character End Select Example: WAP to input day number(dno) and print day name. Private Sub Command1_Click() Dim dno As Integer dno =Val(Text1)
Page - 12

VISUAL BASIC

ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Select Case dno Case 1 MsgBox (Monday) Case 2 MsgBox (Tuesday) Case 3 MsgBox (Wednesday) Case 4 MsgBox (Thursday) Case 5 MsgBox (Friday) Case 6 MsgBox (Saturday) Case 7 MsgBox (Sunday) Case Else MsgBox (Wrong Day number) End Select End Sub

LOOPING STATEMENT
Visual Basic provides mainly three types of looping structure. 1. For Next: This loop is used when you know exactly how many times the code must be repeated. Syntax: For <Counter Var> =Start to End Body Next <Counter Var> Example 1: Loop to print odd numbers between 50 and 100. Solution: For x =50 to 100 step 2 print x Next x 2. Do loop Structure: A do while loop repeat as long as the condition evaluated to true and a do until loop repeat as long as then given condition evaluate to false. a. Do While Loop: This loop is used to when you want to use this condition first. Syntax: Do While (<Condition expression>) Body Loop Example: Count the numbers entered and calculate their sum. Dim sum As Single, num As Single, coun As Integer num =InputBox(enter a number) coun =1 sum =sum + num
ADCPM

VISUAL BASIC

Page - 13

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

Do While num <> 0 num =InputBox(enter a number) num =sum + num coun =coun + 1 loop print coun; print sum b. Do Loop While: In this loop the bodies of loop execute first and then check the condition. Syntax: Do Body Loop while (<cond.exp>) Example: Do num =InputBox (enter a number) sum =sum + num Loop while num <> 0 c. Do Until Loop: It is similar to do while loop but its body repeats until the condition false. Syntax: Do until (<cond.exp>) Body Loop Example: Do until num =0 num =InputBox(enter a number) sum =sum + num Loop d. Do Loop Until: It is opposite to do until loop. In this loop, execute the body first. Syntax: Do Body Loop until (<cond.exp>) Example: Loop to print first n squares lesser than 50. Solution: dim squar as integer, num as integer num =1 squar =num * num Do print squar num =num + 1 squar =num * num Loop until squar>=50 3. While Wend: This loop is replaced do while loop at this time. Syntax: While (<cond.exp>) Body Wend
Page - 14

VISUAL BASIC

ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

While num <> 0 num =InputBox(enter a number) sum =sum + num Wend TERMINATING CONTROL STATEMENT : Some time you may need to exit from a loop even through terminating condition has not reached. In such case, you can use exit statement of Visual Basic. The syntax of exit statement is. Syntax: Exit For for exiting from a For loop Exit Do for exiting from a Do loop Example of Exit For: For x =1 to 10 For y =1 to x If (y mod 3 =0) Then Exit For exit from inner For End If Next y Next x Example of Exit Do: Dim check, counter Check =true: counter =0 initialize variables Do outer loop Do While counter <20 inner loop Print counter Counter =counter + 1 increment counter If counter =10 Then If condition is true Check =false set value of flag to false Exit Do exit inner loop End If Loop Print counter Loop Until check =false exit outer loop immediately

Example:

ARRAY:
An array is a collection of variable of the sometime that are reference by a common name. Type of Array: 1. Fixed Size Array: Syntax: dim <array name> (max Index) as <type> Example: Dim stud (24) as integer. 2. Dynamic Array: A dynamic array can be resized at any time. To create a dynamic array follows these statements Declare the array dim book ( ) as string Allocate the actual number of element redim book (99) Example: dim arr ( ) as integer redim arr (5)
ADCPM

VISUAL BASIC

Page - 15

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

For i =1 to 5 arr (i) =i Next i CONTROL ARRAY: A control array is a group of controls that share the same name and type. Adding and Removing Controls: Adding controls with Control arrays uses few resources then simply adding multiple controls of the same type to a form at design time. Control array are useful if you want to several controls to share code. Example: If there option buttons are created as a control array, the same code is executed regardless of which button was clicked. Using Control Array: Using control array mechanism each new control inherits the common event procedures already written for the array. Example: If your form has several text boxes that each can written receive a date value. Control array can be setup so that all of the text boxes share the same validation codes. PROCEDURES - Writing Modular Codes: A procedure is a name unit of a group of program statements that perform a well define task. There are several types of procedure used in Visual Basic: 1. Sub Procedure: A sub procedure or subroutine or sub is a procedure that performs a task but does not return a value. A sub or subroutine is a block of code that is executed in response to an event. A sub ca receive a values perform a series of statement and change the value of its arguments. These are of two types: General procedure: A General procedure is the one that you create for your own specific purpose. A general procedure tells the application how to perform a specific task. Event procedure: An Event procedure is a procedure associated with a specific event of an object and is named in a way that indicates the object and the event clearly. 2. Function Procedure: A function procedure that performs specific task that returns a value. A function procedure is a separate procedure that can take arguments, perform a series of statements and change the value of its arguments. 3. Property Procedure: Property are the code which when a property of an object gets a new value or when the value is retrieved. Syntax: Sub procedure name (argument list) Statement End sub Event procedure is always a sub procedure.

FUNCTION:
Functions are very similar to subs but there is big difference between the two. This is because a function is a procedure that returns a value. Syntax: Function [procedure name] (argument list) [As type]
Page - 16

VISUAL BASIC

ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Statement End function Type of Function: There are two types of functions: a. User-Define Function: User-define function is created by user. b. Built in function or Library: Built in functions are parts of Visual Basic and these are predefined. User-Define functions have two step processes: Functions Definition: The actual code of a function is written, this is known as declaring function there must be a function call. (The function is invoked this is known as using function). Passing the Parameters: You can use two different methods for passing in parameters to a procedure. 1. Call by value 2. Call by Reference For passing, the call by value parameter by Val Keyword is used and if you want the value by reference then be used By Ref Keyword. Modules: A module is a code container in Visual Basic that contains some procedure and definition. There are three types module in Visual Basic. 1. Form Module: Form module is a module that stores all the procedure and declaration pertaining the single form. The form module is saved with .FRM extension. 2. Standard Module: The standard module are the modules that stores general purpose code of the application that is the code and declaration that are not specific to one single form of the application. The standard module is stores with .BAS extension. 3. Class Module: Class module is a special code module that stores the blue print for user created custom object. Class module is saved with .CLS extension.

INTRODUCTION of MENUS:
A menu is continent and constant way of grouping commands so that they become greatly and easily accessible to user. There are two types of menu: 1. Horizontal menu known as Menu Bar. 2. Vertical menu known as Pop up or pull down menu. Creating Menu in Visual Basic: To add menu in a VB application, you need to invoke Menu Editor. The menu editor of VB is an interactive way to create and modify menu and get to with minimum coding you can also creates shortcut menus through Menu Editor. Design a Menu: To add menu to a Visual Basic application on you need to invoke menu editor. The menu editor of Visual Basic is an interactive way to create and modify menu and get two with minimal coding you can also creates shortcut menus through Menu Editor. Menu Editor: When you open the menu editor first goes to tools menu and then Click on menu editor option or directly press CTRL+E.
ADCPM

VISUAL BASIC

Page - 17

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

Menu Title and Naming Guidelines: When assign captions from menu items you should try the following guide lines. 1. Items name should unique with in a menu. 2. Items name may be single component and multiple words. 3. Each Items name should have a unique mnemonics access characters for user who choose command with Keyboards. The access character should be the first letter of menu title. Ellipses (...): An ellipsis should follow names of command that required more information before they can be completed. Keep items name Short. Access Key: Access keys allow the user to open a menu by pressing the ALT key and typing the designated letter. Once a menu is open, the user can choose a control by pressing the letter assign to it. To assign an access key to a menu control in the Menu Editor: 1. Select the menu item to which you want to assign an access key. 2. In the Caption box, type an ampersand (&) immediately in front of the letter you want to be the access key. Access key allow the user to open a menu by pressing ALT key & assigned letter. Example: Menu Control Caption Cut Copy Paste Delete Select All Time/Date
Page - 18

Caption Property Cu&t C&opy &Paste De&lete Select &All Time/&Date


VISUAL BASIC

Access Keys T O P L A D
ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Creating Popup Menus: A popup menu is a floating menu that is displayed over a form independent of the menu bar. The items displayed on the popup menu depend on where the pointer was located when the right mouse button was pressed; therefore popup menus are also called context menus. A Pop-up menu is a floating menu that is displayed over a form, independent of the menu bar. To create a popup menu, you first need to define a menu through menu editor. And also to make sure that this menu will not display on menu bar, you make it invisible i.e., make sure that Visible check box in Menu Editor is not checked for this menu. After this, you need to write a special code for it that uses Popup Menu method. Any menu that has at least one menu item can be displayed at run time as a pop-up menu. To display a popup menu, use the Popup Menu method. This method uses the following syntax: Syntax: PopupMenu menuname Example: The following code displays a menu named mnufile when the user clicks a form with the right mouse button. You can use the MouseDown or event to detect when the user clicks the right mouse button, although the standard is to use the MouseUp event: Private Sub Form_MouseUp (button as integer, shift as Integer, x as single, y as single) If button=2 then check if right mouse button was clicked. PopupMenu mnuFile display the file menu as a popup menu. End if End Sub MDI (MULTIPLE DOCUMENTS INTERFACE): MDI supports multiple documents at the same time; thus, multiple- documents can be opened simultaneously and worked upon in any MDI application. Example: Ms Office. SDI (SINGLE DOCUMENT INTERFACE): The single document interface supports single document in its window. When you want to open new document then the previous one will be closed. Example: Paint, Notepad, WordPad, etc.

Explorer:
Creating Parent Form: An MDI application must have at least two forms. One parent forms and one or more child forms must be in an MDI. A parent form can
ADCPM

VISUAL BASIC

Page - 19

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

contain many child forms. However, it can use only single parent form. Steps to create Parent form: 1. Start a new project. 2. Click on project 3. Add MDI by clicking. 4. After clicking, an MDI dialog box will be open. 5. Now click on open button. 6. MDI form will now add to your project. Create the application Child form: When you want to create an MDI child form: 1. First go to project and click on it. 2. A drop down menu will be open. 3. In it click on add form Command. 4. And set its MDI Child properties to true. Working with MDI: 1. Start a new project. 2. Click on project 3. Add MDI form (click) 4. After clicking, an MDI dialog box will be open. 5. Now click on open button. 6. MDI form will be get added to your project. a. First, ADD the simple form than one. b. Now click the MDI and open the MNO Editor. c. Create the Menu bar on the MDI Form. d. Now Click on First menu form and go to Code view and type the code as If you want to show the form when you click the first menu option than write the code Form1. Show e. When you run the MDI than get the properties of MDI by Go to Project and then Project Properties Now open the project properties dialog box. Where you go to startup object and Set the MDI Form. f. Now Run the project MDI will show in Run mode. g. Now click on form 1 menu. Form1 will be open in MDI form. Using of Common Dialog Box: The common dialog box control provides a standard set of dialog boxes for operations such as opening and saving files setting print options and selecting color and font etc.
Page - 20

VISUAL BASIC

ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

To use the Common Dialog Box Control follow these Steps: If you have not already done so, at the common dialog control to the toolbox by selecting Components from the project menu. Select Microsoft Common Dialog Control 6.0 in the controls tabbed dialog, and then click the ok button. On the toolbox, click on common dialog control and draw it in a form. Note (It is invisible at the run time). At the run time, use the appropriate method as given below to display the desired dialog. Use of these Method: Method Dialog Displayed ShowOpen Open ShowSave Save ShowColor Color ShowFont Font ShowPrinter Printer ShowHelp Help Use of this Function: If you want to invoke the file open dialog, box and retrieve selective file name. CommonDialog 1. ShowOpen filename=CommonDialog1. FileName File name: File name is the full path name of the file selected by the user. You can also set the File name property to a file name that displayed when the common dialog box is first opened. This allows the user to click the open button to open the preselected files.

WORKING with CONTROLS: The List Box Control: The list box control presents a list of choices to the
user. The choices are presented one or more column if number of items present in exceed what can be displayed in a list box. Scroll bar automatically appear on the control. Major property of list box control are: Property Description Multi-select : The property determines the user can select the lists 1. multiple item or not. It can take following values. 0- Multiple selection not allowed 1- Simple multiple selection. 2- Extended multiple selections Its property set to true. 2. Sorted: This property determines the appearance of the control. 0, 1 3. Style: Common method of List Control: 1. Add item: Syntax: List1. Add item <item>, <Index>
ADCPM

VISUAL BASIC

Page - 21

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

Example: List1. Add item Bareilly, 4 2. Remove item: An item is removed using remove item method on the basic of its index value. Syntax: List1. Remove item <Index> Example: List1. Remove item 0 3. Clear list: List 1. Clear Combo box Control: It is similar to the list box in the since it contains multiple item of which the user may select by but it takes less space on screen. Combo box displays one line with the selected item. Difference between Combo and List Box Control: Is that combo box control allows the user to specify items those do not exist in the list box. 0- Drop down Combo 1- Simple Combo 2- Drop down list.

WORKING with IMAGEBOX and PICTUREBOX:


The Image control is used to display graphic. Image controls respond to the click event and can be used as the substitute or CommandButton. Image control can display graphics in following formats: .pnt (paint file) .icon, Meta files and hence to metafiles curser, .jpeg/.jpg files. Properties Appearance BorderStyle Picture Stretch Description 0 for flat 1 for 3D 0 for none 1 for fixed single This property determines which picture/graphic will be displayed in the Image. You can set it in the design time. The image is resized to fill the area of ImageBox, if the stretch property is set to false. The ImageBox controls behave like the PictureBox with its autosize property.

Loading a Graphic into ImageBox: When you want to load and change image at run time, you can do it by using Picture Property and the load picture function. Image1. Picture =LoadPicture

WORKING with FRAME, OPTIONBUTTON and CHECKBOX : Frame Control: A Frame control is used to group various controls. A Frame
control is a container control, it contains other controls in it; it does not carry out any job/action by itself, it does not respond to any events by itself.
Page - 22

VISUAL BASIC

ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Property Caption Font Name Visible

BorderStyle

Appearance

Description To display text in Frame control To set font, style and size of caption-text of frame. To name the frame. To set a value indicating whether the frame should be visible (value true) or not (value false). This value can be accessed also. Property used to specify the style of border. It can be either 0 - flat or 1 - fixed single. It appears under Appearance category. Property used to set the look of the frame. It can either be 0 - flat or 1 - 3D. It also appears under Appearance category.

CheckBox:
The CheckBox controls are used for offering a smallest of choices from which a user can choose one or more options. A CheckBox indicates whether a particular condition is on or off. You can use check boxes in any application to give users true/false or yes/no options. Because check boxes work independently of each other, a user can select any number of check boxes at the same time. Property Caption Name Alignment Enabled Value Description To specify text to be displayed. To specify the name To determine the alignment of the CheckBox with respect to caption To set a value that determines whether the checkbox can respond to user generated events. To specify the state of checkbox. If its value is true or 1, it means the checkbox is checked, otherwise not.

OptionButton:
The option buttons are used to offer a small set of options from which a user can choose one. Option buttons present a set of two or more choices to user. Unlike check boxes, however, optionbuttons should always work as part of a group; selecting one option button immediately clears all the other buttons in the group. Properties Caption Name Value Style Alignment
ADCPM

Description To display text for OptionButton To name the OptionButton To set the state of Optionbutton, if set to true the Optionbutton appears selected. 0 : for standard 1 : for graphic 0 : for left 1 : for right justify
VISUAL BASIC
Page - 23

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

WORKING with TIMER CONTROL


The Timer control is a control that is actually invisible at run time but is used to perform some task or activity at regular intervals. Instead of making a noise or beeping, the timer control executes code, when the specified interval is complete. The Timer control counts down repeatedly. As long as the Enabled property is set to True. The Timer control is designed to work with very small amounts of time; the maximum setting is just a little longer than a minute. An important property of Timer control is the Interval. The Interval property contains a value that must range from 1 to 65,535. The value is in milliseconds, so an Interval value of 500 would equate to half a second. The Timer control generates only one event: the Timer event. Example: Designing a digital clock using Timer control as shown below: Solution: 1. Create a Form with 5 Labels on it:

2. Now set the properties of these Labels as specified below:


Control Label1 Property Name Caption Font Alignment Caption Alignment Font Name Caption Font Alignment Caption Alignment Font Name
VISUAL BASIC

Label2

Label3

Label4

Label5
Page - 24

Setting hr 00 TmsRoman,36pts,Bold 2-center : 2-center TmsRoman,36pts,Bold min 00 TmsRoman,36pts,Bold 2-center : 2-center TmsRoman,36pts,Bold sec
ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Caption Font Alignment

00 TmsRoman,36pts,Bold 2-center

3. Now set the Form properties as follow:


Caption Digital clock 4. Now click on Timer control and add one timer control on your form. The form would now look somewhat like the one shown below:

5. Now add following code in the Timer event of Timer control:


Private Sub Timer1_Timer() Dim hh As Byte Dim mm As Byte Dim ss As Byte ss =Int(sec.Caption) mm =Int(min.Caption) hh =Int(hr.Caption) ss =ss + 1 If ss =60 Then ss =0 mm =mm + 1 End If If mm =60 Then mm =0 hh =hh + 1 End If If hh =24 Then hh =0 End If sec.Caption =Format(ss, 00) min.Caption =Format(mm, 00) hr.Caption =Format(hh, 00) End Sub
ADCPM

VISUAL BASIC

Page - 25

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

6. Save your project and run it by pressing F5. Now running application will
look somewhat like the one shown in given screenshot. Scroll Bar: A scroll bar is a long strip with a moveable indicator that lets the user selects a value between the two ends of a control. The scroll bar controls presents in two forms: 1. Horizontal 2. Vertical The left and upper ends are for the minimum value and right and lower ends are for the maximum value.

To add a component ADODC: (Active data object dialog control) 1. Click on project. 2. Go to component and click on it. 3. Component dialog box will be open. 4. From where select the ADO data control 6.0 OLEDB. 5. Write the check box. 6. Apply and close. To add ADODC with Access database: 1. Right click on ADODC. 2. Click on ADODC property. 3. Click on build event. 4. Now open a data link property dialog box. 5. Select the MS JET 4.0 OLEDB provider. 6. Click on Next. 7. Now select the database by clicking on 3dotes (). 8. Open a select Access database dialog box will be open. 9. Select BIBLIO. 10. Click on open. 11. Check the Blank password. 12. Click on test connection. 13. A message will show test connection succeeded.
Page - 26

VISUAL BASIC

ADCPM

BLY / June - 2010

COMPUTER PROGRAMMING

Module 5

Click on ok. Click on Advanced. Select read and write permission. Now click ok. Click on Record source. Select the cmd table. Apply and ok. Now select all the text boxes and go to properties. Click on Data source select ADODC1 Apply the Data field property one by one. Working with Recordset Properties and Methods: Coding for ADODC: ADODC1. Recordset. MoveNext ADODC1. Recordset. MovePrevious Updating Recordset Data: ADODC1. Recordset. Update What is ADO? ADO stands for ActiveX Data Objects.ADO is the successor to DAO (Data Access Objects)/RDO (Remote Data Objects).ADO is flattened it contains fewer objects and more properties, methods and events.

14. 15. 16. 17. 18. 19. 20. 21. 22. 23.

Working with DriveList, DirList and FileListBox:


The three available controls are the drivelistbox, dirlistbox and filelistbox control, let you access the computers file system. Using these controls the user can troubles the host computer file system, locate any folder or files or any hard disc even on network drives the files controls are independently of one another and each can exist on its own, but they are rarely used separately. Functionality of these three files: 1. DriveListBox: It displays the name of the drives within and connected to the PC, the basic property of this control is Drive. 2. DirListBox: It displays the folder of the current time; the basic property of this control is Directory, which is the name of the folder. 3. FileListBox: It displays the files of the current folder. The basic property of this control is also called path. The path name of the folder, whose files are displayed and other important property is pattern which lets you specify which file will be displayed with a file matching string such as *.doc or *.txt; you may write code in the change event procedure(Drive1_change()) Dri1.path=Drive1.Drive Change event procedure of DirListBox: namely Dir1 file.path=Dir1.path SHAPE and LINE Controls: Shape and Line control are useful for drawing graphical elements on the surface of a form. These controls do not support any type of events; they are strictly for
ADCPM

VISUAL BASIC

Page - 27

Module 5

COMPUTER PROGRAMMING

June - 2010 / BLY

decorative purpose. The main properties of Shape and Line controls are: 1. BorderColor 2. FillColor 3. BorderStyle 4. BorderWidth 5. FillStyle. Shape is also an important property. It can be displayed rectangle, square, oval, circle, rounded rectangle and rounded square. Working with OLE Container Control The OLE container control is a way of supporting Object Linking and Embedding. Object Linking and Embedding is a way to link applications. The object can be a graphic image or a piece of text or a table or even a sound clip.OLE provides the users the capability to create compound document that contain one or more objects from other applications. Properties of OLE Container Control : Property Description Class Returns the name of the application that produced the object. SourceDoc Returns the data or a reference to the data. SourceItem Returns the image of the data. This applies to linked objects only. SizeMode Returns or sets a value specifying how the OLE container control is sized or how its image is displayed when it contains an object. OLETypeAllowed This property setting determines whether the source document is embedded in or linked to the OLE container control. Creating an Embedded Object: 1. Create empty OLE container at design time. Draw an OLE container control on your form by dragging. When Insert Object dialog box appears. Choose Cancel or right click the control and select Delete Embedded Object if an embedded object already exists. 2. Load embedded object at runtime. You can create an embedded object at runtime by using CreateEmbed method in an empty OLE container control. The CreateEmbed method takes two parameters: SourceDoc and Class name of the object. You can leave the source document parameter empty if you want to a blank object. This line of code creates a Paintbrush picture object in an OLE control named OLE1: OLE1.CreateEmbed ,Paint.Picture
Note: If you find any typing or printing error/s, inform immediately to your branch office.
Page - 28

VISUAL BASIC

ADCPM

Das könnte Ihnen auch gefallen