Amazon

Wednesday, May 18, 2016

Working with MDI Applications and Creating Menus with VB2010

MDI OVERVIEW
·         In graphical user interfaces, a multiple document interface or MDI is one whose windows reside under a single parent window.
·         Such systems often allow child windows to embed other windows inside them as well, creating complex nested hierarchies.
·         An MDI most often in applications where the user might like to have multiple forms or documents open concurrently.
·         Word processing applications (like Microsoft Word), spreadsheet applications (like Microsoft Excel), and project manager applications (like Microsoft Project) are all good candidates for MDI applications.

·         MDI is also handy for a large application that provides a simple mechanism for closing all the child forms when the user exits the application.

Figure shows a basic MDI application in use. 
CREATING AN MDI PARENT FORM
·         To create an MDI parent form, simply take one of an existing windows form and set its IsMDIContainer property to True. This form will now be able to contain other forms as child forms. An application may have one or more container forms within the application.

  1. Open Visual Studio 2010
  2. Create a new Windows application project.
  3. Set the name of the project to whatever necessary.
  4. Rename the form that is created automatically to frmMain.vb.
  5. With the frmMain selected, set the form's IsMdiContainer property to True.
  6. Set the WindowState property to Maximized.
CREATING MENUS

To add menus to MDI parent form
  1. Double-click the MenuStrip tool in the Toolbox window to add a new object named MainMenu1 to the form tray. 
  2. At the top of the MDI parent form, click the box with Type Here in it and type &File. Inserting an ampersand (&) into a menu will set the succeeding character as its access key.
  3. Press Enter to move to the next menu item and type &Products.
  4. Press Enter to move to the next menu item and type a hyphen (-).         Tip Rather than using the "-" to indicate a divider in the menu, you can insert the next menu item (Exit, in this case), and then right-click the new item. Select "Insert Separator" from the context menu, and Visual Studio .NET will insert a separator above the current item for you.
  5. Press Enter and type E&xit.        Figure shows a drop-down menu on main form.
    The menu designer allows you to type your menu structure in a WYSIWYG fashion


    To the right of the File menu and at the same level, you'll see another small box with the text, Type Here. Click it and type the following menu items by pressing Enter after each one.
    • &Edit
      • Cu&t
      • &Copy
      • &Paste
    Once more to the right of the Edit menu and at the same level, add the following menu items in the same manner.
    • &Window
      • &Cascade
      • Tile &Horizontal
      • Tile &Vertical
      • &Arrange Icons
    CREATING NAMES FOR EACH MENU
                After creating all the menu items, it is needed to set the Name property for each. (It Is important to choose a name which can be easily understood from within the code in order to easily refer to these items.) Instead of clicking each menu item one at a time and then moving over to the Properties window to set the Name property, Visual Studio provides a shortcut: Right-click an item in the menu, then select Edit Items from the context menu. Now simply click each menu item and set the name property directly on each menu.
    Use the following names for the menu items:
    • mnuFile
      • mnuFProducts
      • mnuFExit
    • mnuEdit
      • mnuECut
      • mnuECopy
      • mnuEPaste
    • mnuWindow
      • mnuWCasade
      • mnuWHorizontal
      • mnuWVertical
      • mnuWArrange

    Test out the application: Press F5 and see the main MDI window appear with the menu system in place.

    DISPLAYING  A CHILD FORM
                To add the code that displays the child form, frmProducts, make sure the main form is open in Design view, and     on the File menu, double-click Products. Visual Studio will create the stub of the menu item's Click event handler             automatically. Modify the procedure so that it looks like the following:

    Private Sub mnuFProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFProducts.Click
        Dim frm as new frmProducts()  
              frm.MdiParent = Me
        frm.Show()
    End Sub

    This code declares a variable, frm, which refers to a new instance of the frmProducts form in the sample project. Then, set the MdiParent property of the new form, indicating that its parent should be the current form (using the Me keyword). Finally, the code calls the Show method of the child form, making it appear on the screen.
    • If the MdiParent property of the new child form is not set "frm.MdiParent = Me", the form will simply load as a new normal form, outside the MDI parent.

    -------------------OR simply load a form as child by not creating an instance of the frmProduct-----------------------

    Private Sub mnuFProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFProducts.Click
              frmProducts.MdiParent = Me
              frmProducts.Show()
    End Sub


To download my book "How to Develop Information System using Visual Basic 2010:
A beginner's step by step guide." visit https://www.smashwords.com/books/view/637336

No comments:

Post a Comment