1 / 41

Chapter 7

Chapter 7. Multiple Forms, Standard Modules, And Menus. 7.1 Introduction. Chapter 7 Topics. How to add multiple forms to a project How to create a standard module To hold procedures and functions That is not associated with a specific form Creating a menu system Context menus

Download Presentation

Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 Multiple Forms, Standard Modules, And Menus

  2. 7.1Introduction

  3. Chapter 7 Topics • How to add multiple forms to a project • How to create a standard module • To hold procedures and functions • That is not associated with a specific form • Creating a menu system • Context menus • With commands and submenus that the user may select from

  4. 7.2Multiple Forms Visual Basic .NET Projects May Have Multiple Forms If One of the Forms Is the Startup Object, It Is Displayed When the Project Executes The Other Forms Are Displayed by Programming Statements

  5. Form Names • Each Form has its specific name • This name is set/changed with the Name Property • Each form also has a file name (.vb) • To change the file name: • Right click its entry in the Solution Explorer • Choose Rename

  6. Adding a New Form • Add New Item on the tool bar • The Add New Item dialog box appears • Click on Windows Form under Templates • Change the default name if you wish • Click Open

  7. Switching between Design and Code • The Design window has the following tabs • frmMain.vb[Design] Form Design Itself • frmError.vb[Design] Error Form Design • frmMain.vb Main Code • frmError.vb Error Code

  8. Classes and Instances • When you create a Form at design time, you are just creating a general description of a Form(It does not actually exist at that moment) • Analogy: It is a blueprint of a Form • An Instance must be created to actually have a Form at run time • Analogy: What the blueprint describes is actually built

  9. Creating an Instance of a Form • It must be declared, syntax: • For example: • It is still not visible, but a form object has been instantiated • Refer to it using the variable errorForm Dim ObjectVariable As New ClassName() Dim errorForm As New frmError()

  10. Modal Form/ShowDialog Method • When a Modal Form is displayed, no other form in the application can receive the focus until the modal form is closed • Use the ShowDialog Method to display a modal form: errorForm.ShowDialog()

  11. Modeless Form/Show Method • A Modeless Form allows the user to switch focus to another form in the same application • Use the Show Method to display a modeless form: errorForm.Show()

  12. Closing a Form • A form may close itself using the Close Method and referring to itself as "Me": • As in Me.Close() Private Sub btnClose_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnClose.Click Me.Close() End Sub

  13. Hiding a Form • Closing a Form eliminates it from memory • To simply make it not visible, use the Hide Method: • To bring it back use the ShowDialog() or Show() Methods Me.Hide()

  14. More About Modal and Modeless statement; messageForm.ShowDialog() ' Statements below will ' not execute until the ' Form is closed statement; statement; messageForm.Show() ' Statements below will ' execute right after the ' Form is displayed statement;

  15. A Form's Load Event • The Load Event is triggered just before the form is initially displayed • If code needs to execute at this time, it must be contained in that event handling procedure: Private Sub frmMain_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load End Sub

  16. A Form's Activated Event • The Activated Event is triggered when the user switches to the from from another form or another application • It is also triggered after the Load Event

  17. A Form's Closing Event • This Event is triggered as the form is in the process of closing, but before it has closed • For instance, one might want to ask the user if they really want the form closed

  18. A Form's Closed Event • The Closed Event is triggered after a form is closed • Note that it is now too late to prevent the Form from being closed (it is already)

  19. Accessing Objects on a Different Form • When code in a form refers to an object, it is assumed that that object is in the same form • To refer to an object in another form, preface the object name with the variable name associated with that form: Dim greetingForm As New frmGreeting() greetingForm.lblMessage.Text = "Hello!" greetingForm.ShowDialog()

  20. Class-level Variables and Multiple Forms • Class-level variables are Private by default • This means they are not accessible by code in other forms • If you wish them to be, they must be declared with the Public qualifier: Public total As Single ' Instead of the declaration ' Dim total As Single

  21. Procedures Have Similar Access Rules • Procedures, by default, are Public • They can be accessed by code outside of their Form • To prevent this from happening, declare them to be Private

  22. 7.3Standard Modules A Standard Module Contains Code - Declarations and Procedures - That Are Used by Other Files in a Project

  23. Furthermore, Standard Modules • Are not associated with a Form • Contain no Event Procedures • Are saved in files with a .vb extension • Are used to hold code that is used by multiple Forms, or for command-line programs

  24. Standard Module Syntax • A Module will contain Sub procedures and Functions, they can be • Private - only used by functions in that Module • Public - can be called from outside of the Module Module ModuleName [Module Contents] End Module

  25. Module Level Variables • These are declared within a Module • But not within Functions or Sub procedures in that Module • If declared Dim or Private, their scope is the Module (called module scope) • If declared Public, their scope is the application (called global scope)

  26. Application with No Startup Form • Designate a Public Sub procedure named "Main" as the startup object • It must be in a Standard Module • When the application starts • No Form will be displayed • "Main" will be given control

  27. 7.4Menus Visual Basic .NET Allows You to Create a System of Drop-down Menus for Any Form in Your Application You Use the Menu Designer to Create a Menu System

  28. Components of a Menu System, I Menu Name Shortcut Key Checked Menu Command Menu Command Separator Bar Submenu

  29. Components of a Menu System, II • Menu Names - each drop-down menu has a name • Menu Command - the list of actions in the drop-down list • Shortcut Key - a key or combinations of keys to activate the command from the keyboard

  30. Components of a Menu System, III • Disabled Menu Command - a command that is not applicable is grayed out (and is not selectable) • Checked Menu Command - some commands are toggles - checked when on, unchecked when off

  31. Components of a Menu System, IV • Submenu - a command may lead to another menu • Separator Bar - a horizontal bar used to separate groups of commands

  32. MainMenu Control • When used, the MainMenu control appears in the component tray (at the bottom of the Design Window) • It is composed of MenuItem Objects, such as: • Menu Name • Menu Command • Separator Bar • Sub-Menu Command

  33. MenuItem Names • Should begin with "mnu" • Then by convention are spelled, specifying their hierarchical position: • mnuFile • mnuFileSave • mnuFilePrint

  34. MenuItem Text Properties • The text property is the text that appears on the menu • Plus if there is a access key, it is preceded with an ampersand, e.g.Object NameText Property mnuFile &File mnuFileSave &Save mnuFileExit E&xit

  35. Menu Designer • The Menu Designer speeds creation of menus with a fill in the box with text scheme: Enter the next menu name Enter first command in the File menu

  36. Shortcut Keys • These are the key shortcuts that get one directly to the command action without going through the menu system(e.g., CTRL-C for Edit/Copy) • These are set via the Shortcut Property of each menu item

  37. Disabled MenuItem Objects • The status of a menu item (enabled vs. disabled) is controlled by the Enabled property: True is Enabled mnuEditPaste.Enabled = True

  38. Adding Separator Bars • Either make the menu item’s Text property equal to "-" (hyphen) • Or right-click an existing menu item and select Insert Separator (it will be inserted above the menu item)

  39. Inserting, Deleting, RearrangingMenu Items • To Insert a new menu item above an existing one, right-click to use the context sensitive menu • To Insert a new item at the end, use the Menu Designer • To Delete a menu item, right-click/Delete • To Rearrange menu items, click and drag them in the Menu Designer

  40. Standard Menu Items • In general follow the conventions that the majority of application menu systems use • Specifically: • 'File' is the leftmost menu item • The File menu has an 'Exit' command • 'Help' is the rightmost menu item • The Help menu has an 'About' command

  41. Context Menus • To establish a pop-up menu when a control is right-clicked: • Add the ContextMenu control to the component tray • Build the menu system with the Menu Designer • Add a Click Event procedure • Then associate the menu with the control by setting the control's ContextMenu property to the established menu

More Related