creating menus in your programs

Upload: nyabs-yohana

Post on 14-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Creating Menus in Your Programs

    1/26

    Creating Menus in Your Programs

  • 7/30/2019 Creating Menus in Your Programs

    2/26

  • 7/30/2019 Creating Menus in Your Programs

    3/26

    Using Visual Basic's ApplicationWizard

    Visual Basic 6 has improved the Application Wizard to allow for the creationof fully customized menus directly in the wizard. In the earlier versions of the wizard, you could select from only a limited set of standard menuoptions to include in your application. This improvement makes the wizard auseful tool and a good starting point for building and understanding your firstmenu.

    The primary benefit of VB's Application Wizard is that it can make menusthat have the Windows standard features already loaded. You simply selectthe features you want from the template provided. They're also alreadyarranged in the Windows standard layout.

    Limitations of the Application Wizard You can't use the Application Wizard to modify existing projects. Many third-

    party support programs add more functionality to your project without havingyou do more programming. You can also use the add-ins that come withVisual Basic, such as the Menu Editor.

  • 7/30/2019 Creating Menus in Your Programs

    4/26

    The Application Wizard is a straightforward toolfor building a new application. This means thatthe Application Wizard is used to build a

    functional application shell with standardfeatures. If you want additional features, youhave to program them yourself or use the MenuEditor to add them. After you click Finish in the

    Application Wizard and the base program isgenerated, you're on your own to make changesto the program by using the Menu Editor

  • 7/30/2019 Creating Menus in Your Programs

    5/26

    Build a simple menu with theApplication Wizard

    1. Start the Application Wizard through the default dialog that openswhen Visual Basic 6.0 starts or by choosing New Project from theFile menu

    Double-click the Application Wizard icon to start the wizard 2. The wizard's Introduction dialog allows you to reuse the answers

    you saved during a previous Application Wizard session Leave thedefault choice and choose Next . 3. In the Interface Type dialog, select the type for the initial

    application screen For this sample application, select SingleDocument Interface . Leave the default project name and chooseNext .

    4. A Windows standard default menu is created for you to begin tomodify. Not all possible choices have been selected in the initialmenu, After you make your modifications, choose Next .

    5. The Application Wizard allows for the customization of a toolbar,resource file, browser, database connectivity, and other templates.For this example, skip these dialogs; choose Next five times to getto the final dialog.

  • 7/30/2019 Creating Menus in Your Programs

    6/26

    6. In the last dialog, you can save your profile Enter a name for your profile thatwill be easy to relate to your application.

    After you enter the name, choose Finish to complete the Application Wizard.(Clicking Finish on any of the earlier

    dialogs in the wizard bypasses the optionto save the profile for future use.)

  • 7/30/2019 Creating Menus in Your Programs

    7/26

    Using Visual Basic's Menu Editor The Menu Editor allows you to create menu bars from scratch or

    modify already created menus. As with the Application Wizard,these menus are located at the top of a form, with associated drop-down submenus or pop-up menus that users typically access byright-clicking.

    Make a simple menu with the Menu Editor 1. Open a new project. Save the project with a new filename. Rename

    the default form (with a name such as frmMenu) and save it to a newfilename, such as frmMenu.frm.

    Don't press Enter yet! Press Tab or use the mouse to move between text boxes in the

    Menu Editor; otherwise, you cause the Menu Editor to create a newmenu item.

  • 7/30/2019 Creating Menus in Your Programs

    8/26

    Using ampersands Including an ampersand in the menu caption forces the character that

    comes after it to be underlined when it appears in the menu. Theunderlined character becomes the hot key, which users can press(combined with the Alt key) to make a selection. For example, withthe F underlined on the File menu, users can press Alt+F to pulldown that menu

    2. Open the Menu Editor by clicking the Menu Editor button on theStandard toolbar. If the form doesn't have the focus, the Menu Editor icon is grayed out.

    3. In the Menu Editor dialog, type &File in the Caption text box andmnuFile in the Name text box. Click Next .

    4. Click the button with the arrow pointing to the right. This is the indentbutton.

  • 7/30/2019 Creating Menus in Your Programs

    9/26

    5. Type E&xit in the Caption text box and itmExit in theName text box. Your menu should appear in thedialog as shown in Figure 13.6.

    6. Click OK.

    7. The menu you created is embedded in the form Dropdown the File menu that you just created and clickExit . The code window for the itmExit_Click() event

    procedure appears.8. Add the Unload Me statement to the itmExit eventprocedure

    9. Press F5 to run the code

  • 7/30/2019 Creating Menus in Your Programs

    10/26

  • 7/30/2019 Creating Menus in Your Programs

    11/26

    You then enter identifiers for the Caption and Name for each menu item. (The Caption isactually the screen name of the item, as it appears in the Menu Bar or within the drop-down menu. The Name is used only in Visual Basic code it is not displayed when theapplication is running.) The Caption will appear in the large area at the bottom of theMenu Editor as well as within the Caption field. You may either press the Enter key or

    click on the Next button after the information has been entered for each menu item.The menu headings must be flush left within each line. Items that appear withineach menu must be indented one level, as indicated by four ellipses precedingeach item. The indentation is accomplished using theright-arrow button. Click once to indent one level (four ellipses). The oppositeaction, i.e., moving an indented item to the left, is accomplished with the left-arrow button.The relative ordering of each menu component can be altered using the up- anddown-arrow buttons. Thus, to move an entry ahead of the two preceding entries,highlight the entry to be moved and click on the up-arrow button twice. Inaddition, a menu component can be inserted by highlighting the item that will

    appear below the insertion, and clicking on the Insert button. Similarly, a menucomponent can be deleted by highlighting the component and clicking on theDelete button.Remember that it is the indentation pattern that distinguishes the menu heading from the corresponding items in the drop-down menu. Also, it is the order of theentries that distinguishes one set of menu components

    from another. (This may sound more complicated than it really is, as shown inthe following example.)

  • 7/30/2019 Creating Menus in Your Programs

    12/26

  • 7/30/2019 Creating Menus in Your Programs

    13/26

    Property Value/Type Description

    Caption String The text that appears on the menu bar.

    Checked

    Boolean

    Puts a check mark before the Caption string of a menu item.

    Enabled Boolean Doesn't gray out the Caption string if True.

    Name String The name of the object--available only atdesign time.

    Property Value/Type Description Shortcut N/A A key combination that allows you to access the

    menu item's functionality. You can choose thissetting only at design time from a list thatappears in the Shortcut drop-down list in theMenu Editor.

    WindowList Boolean Makes a top-level menu in an MDI form displaya list of windows open in that MDI form--available only at design time.

  • 7/30/2019 Creating Menus in Your Programs

    14/26

    Adding Access Keys to MenuItems

    In addition to clicking a menu item to perform a task, you can accessa menu item's functionality by using access keys . Access keys allowusers to perform menu selections by pressing Alt and then theassigned letter of the access key. After a menu is open, users selectfrom the menu by pressing the access key for the desired menuchoice.

    An access key is denoted by an underlined character in a menuitem's Caption--for example, the F in File on a standard Windowsmenu bar. When you press Alt+F, the result is the same as if youhad clicked File on the menu bar.

    To denote an access character, put an ampersand (&) before thecharacter you want to use. the ampersand indicates that the letter F

    in File will be underlined.

    Shortcut keys are assigned in the Menu Editor before compiling the program and can't be changed in the runtime environment.

  • 7/30/2019 Creating Menus in Your Programs

    15/26

    Adding Shortcut Keys to MenuItems

    Using shortcut keys (also known as accelerator keys ) is another way of performing menufunctions from the keyboard. You set a shortcutkey combination by choosing a combinationfrom the Menu Editor's Shortcut drop-down list

    Shortcut keys work anywhere in the program,whether or not the menu item is visible. That'swhy they're called shortcuts--they're active allthe time, and users can jump to the functionwithout going through the menu selectionprocess.

  • 7/30/2019 Creating Menus in Your Programs

    16/26

    Adding Separator Lines toMenus

    You add separator lines to a menu by typing a single hyphen (-) in theMenu Editor's Caption text box.

    &Continents

    ....&Africa

    ....As&ia

    ....&Europe-....&North America....&South America-....A&ustralia-

    ....An&tarctica

  • 7/30/2019 Creating Menus in Your Programs

    17/26

  • 7/30/2019 Creating Menus in Your Programs

    18/26

    SUBMENUS A menu item may have a submenu associated

    with it. Placing the mouse over the menu item

  • 7/30/2019 Creating Menus in Your Programs

    19/26

  • 7/30/2019 Creating Menus in Your Programs

    20/26

    Creating Pop-Up MenusThere are two types of menus: menu bars and pop-up menus. A menu bar is

    the type that you made with the wizard and edited in the Menu Editor--asequence of menus embedded on a form. A pop-up menu is, as the nameimplies, a menu that pops up from somewhere on a form. When you right-click in the Windows Explorer, for example, a pop-up menu appears. Youcan make any menu appear as a pop-up menu by using a form'sPopupMenu method:

    PopupMenu mnuFile A menu command can exist within a menu bar and as apop-up menu, if you wanYou need to create a form to go with the pop-upmenu because the PopupMenu method must be part of a form. Thefollowing code demonstrates how this feature works:

    Private Sub Form_MouseDown (Button As Integer, _ Shift As Integer, X AsSingle, Y As Single

    If Button = 2 Then

    PopupMenu mnuFileEnd If End Sub

    For this code to work, you need to create a form that includes a Menu controlto be named mnuFile, which must have at least one submenu. You need to

    program this code into the form's Declarations section and press F5 to runit.

  • 7/30/2019 Creating Menus in Your Programs

    21/26

    MORE ABOUT THE MsgBox FUNCTION

    integer variable = MsgBox(string, integer, title)The value of the integer argument (default 0) defines the command

    buttons that appear within the dialog box. Also, title represents a string that will appear in the message boxs title

    bar. Its default value (if not included as an explicit argument) will bethe project name.

  • 7/30/2019 Creating Menus in Your Programs

    22/26

    The value returned by the MsgBox function will depend upon the particular command button selected by the user during program execution. The possible values aresummarized below.

  • 7/30/2019 Creating Menus in Your Programs

    23/26

    THE InputBox FUNCTION The InputBox function is similar to the MsgBox function.

    However, this function is primarily intended to display adialog box that accepts an input string, whereas theMsgBox function is primarily intended to show an output string.

  • 7/30/2019 Creating Menus in Your Programs

    24/26

    string variable = Input Box(prompt, title, default) The first argument (prompt) represents a string that

    appears within the dialog box as a prompt for input. Thesecond argument (title) represents a string that will appear in the title bar. Its default value (if not included as an explicit argument) will be the project name. Thelast argument (default) represents a string appearing appearing initially in the input boxs text box. The default

    string will be empty if this last argument is not included inthe function reference.

  • 7/30/2019 Creating Menus in Your Programs

    25/26

    Private Sub Command1_Click()Dim CustomerName As String, L1 As String, L2 As String

    Dim AcctNo As Integer, Verify As Integer L1 = "Customer Name: " L2 = "Account Number: " DoLabel1 = L1

    Label2 = L2 CustomerName = InputBox("Please enter your name:", "Customer Name")Label1 = L1 & CustomerName

    AcctNo = Val(InputBox("Please enter your account number:", "Account Number"))

    Label2 = L2 & Str(AcctNo)'Process the account

  • 7/30/2019 Creating Menus in Your Programs

    26/26

    Verify = MsgBox("Is this correct?", 3,"Verify")Loop While Verify = 7

    If Verify = 2 ThenLabel1 = L1Label2 = L2 End If End SubPrivate Sub Command2_Click()

    End End Sub