Vous êtes sur la page 1sur 6

Visual BASIC Fundamentals

Opening Visual BASIC version 6 (VB6). VB6 is located at: C:\Program Files\Microsoft Visual Studio\VB98\VB6.exe It should start up with a banner that shows Service Package 6 is installed. First select from the New Project form (Figure 1) either the New, Existing or Recent tab and open a project (extension .vbp). One important option is to save changes (or not if you choose) before running a program. Select: Tools | Options... then select the Environment tab. Push the button Prompt To Save Changes in the When a program starts: frame. If the program already exists and has many forms or modules you may need to expand the branches in the Project window at the right of the screen. Click the plus box beside the Forms and Modules branches of the project tree (Figure 2). Figure 1. New Project form

The Properties window is usually in the right and below the Project window (see Figure 3). This area changes Figure 2. Project window every time you click on an object in the current main (centre) window. The left window (Figure 3), called the Toolbox, shows the available objects from the various components (.ocx files) that have been selected or are always available to VB6 projects. Adding Components Press Ctrl-T or click on Project | Components... Ctrl+T to open the Components form (Figure 4). Check the boxes beside any components you want to use in your project. Below is a list of commonly used components: Microsoft components: Microsoft Chart Control 6.0 (SP4) - for graphing and charting Microsoft Comm Control 6.0 - for communicating with the serial port Microsoft Common Dialog Control 6 (SP6) - for opening files, printers Microsoft Flexgrid Control 6.0 - for spreadsheet-like grids 1

2 National Instruments, Measurement Studio (Component Works): National Instruments CW 3DGraph 6.0 - 3D graphing National Instruments CW Analysis 6.0 - DSP, stats, complex math National Instruments CW DAQ 6.0 - data acquisition functions National Instruments CW UI 6.0 - 2D graphing, toggles, data entry

Figure 3. Visual BASIC 6.0 programming environment

Figure 4. Components window

3 Adding Objects To add an object, such as a command button, to a form click on the object from the Toolbox. Next, size the object if appropriate and move it to a suitable location. You can also edit any of its properties in the Properties window. It is a good practice to edit the objects name in the (Name) area of the Properties window. There is a general practice of starting the name with a standard abbreviation. Here are some examples: Object Command button List box Combo box Options button Text box Abbreviation cmd lst com opt txt Form Frame Check box Filelist box Label Object frm fra chk fil lbl Abbreviation

Of course, many others exist. Note, some items cannot be resized and/or do not appear when the program runs. For example, the Common Dialog object and the DSP object of Measurement Studio. These objects add routines that are called from the code itself. Adding Code to an Object You can add code to each object by double-clicking on the object in the form. Another way is to click on the leftmost icon (View Code) in the Project window (Figure 5). The centre icon (View Object) switches back to the form layout. Note that each form has a number of events that trigger the code. For example, passing the mouse over the object, pressing a key, clicking or double-clicking the item when the program is running. To apply the code to a specific event or procedure, move the mouse to the listbox at the top right of the code window. The left listbox is a list of all the objects currently in the selected form. Here is a list of events/procedures common to objects: Change Dragover Load Resize Click Keydown Mousedown Unload Dblclick Keypress Mousemove Dragdrop Keyup Mouseup Note, not all objects have these properties and there are usually many others for each object. You can also have subroutines that are not associated with a particular object and can therefore be used by various objects. These can be viewed by clicking on the (General) item at the top of the left (Object) listbox and the moving the mouse over the right listbox. This changes the listbox to a (Declarations) listbox and shows all the code that is not associated with any object.

Figure 5. Environment with main window in text (View Code) mode Adding a Menu to a Form (Menu Editor) This feature can only be realized when you are in form layout mode. Press Ctrl-E, click on the third icon (Menu Editor) or select Tools | Menu Editor... Ctrl+E from the menu. The form (Figure 6) requires that you give each menu item a label (caption) and an object name. To create a hotkey precede the hotkey letter by an ampersand (&) in the caption. The object name is usually prefixed with mnu, e.g., mnuSave. You can allow indexing (i.e., have a series of items under a single menu object, otherwise you will have a different object name for each menu item. You can also select by checkboxes whether the item is Checked, Enabled, Visible or in a Windowlist. Generally check the Enabled and Visible boxes. The arrow keys permit moving the item up or down or indenting or unindenting the item. Click OK to exit and keep the menu.

5 To add code to a menu item, exit the Menu Editor and from the form layout mode click on the menu item as you would during program execution. You can the enter code as usual. If an item has been indexed you need to handle which index was selected. This is commonly done by the Select Case function. I.e.,
Select Case Index Case 0 ... Case 1 ... End Select

Note the word Index is the word that appears in the menu definition statement. It Figure 6. Menu Editor form appears after you have clicked on the menu item. This statement takes the form:
Private Sub mnuName_Click(Index As Integer)

The word Private means the menu item is available only to the current form. The word Sub means this is a subroutine and not a function. The _Click is the event (a mouse click) the starts the menu object to execute. The name Index will holds the index of the menu item that was clicked. Index begin from 0 and increase incrementally. The Menu Editor requires that items are in order and in sequence. The words As Integer means the index is an integer. To add a separator within a series of menu items enter a dash (-) as a caption. You will need to also enter a menu name such as mnuBar1, mnuBar2 etc. Separators are only permitted in submenus not the main (horizontal) menu. Modules Modules are areas where common code that is not associated with forms may be stored. Subroutines or declarations statements that are global (i.e.,) are used by more than one form are placed in modules. One module that is very useful, contains the statements necessary to start programs such as Notepad or Windows Explorer or to open and program that is associated with an installed program. This module is called, Winshell.bas. It contains the statement:
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal fsShowCmd As Long) As Long

6 This statement can only be used with Windows98 or later. For example, to start Notepad from the program, include the following statement:
RC = ShellExecute(frmMain.hWnd,"open","notepad.exe","","",vbNormalFocus)

Note the word, frmMain, must be the same name as the form from which the program is called. The word, vbNormalFocus, means the program will have focus after it starts. You can also start any program maximized (vbMaximizedFocus) or minimized (vbMinimizedFocus). RC must be declared as a Long variable (double precision) and will contain a number that identifies if an error occurs. An alternate method that has fewer options and does not require Winshell.bas is:
RC = Shell("explorer.exe",vbNormalFocus)

This command will start Windows Explorer. To view an Acrobat .pdf file use:
RC = ShellExecute(frmMain.hWnd,"open","c:\temp\readme.pdf","","", vbNormalFocus)

This command links to a URL with the default browser:


RC = Shell("http://www.health.uottawa.ca/biomech", vbNormalFocus)

Vous aimerez peut-être aussi