#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    44
    Rep Power
    0

    Coding a Menu without the use of the toolbox


    As i said i'm coding without the use of the toolbox and i have run into a snag. I need to be able to find out which item im clicking on for the event and i am a little confused as to how to go about this.
    Here is what i have so far:
    Code:
        Dim mainMenu1 As New MainMenu
        Dim WithEvents menuItem1 As New MenuItem
        Dim WithEvents menuItem2 As New MenuItem
    Code:
            'adding a menu
            'changing the text for the first menu item
            menuItem1.Text = "File"
            'changing the text for the second menu item
            menuItem2.Text = "Books"
    
          
            Menu = mainMenu1
    
            With mainMenu1
                .MenuItems.Add(menuItem1)
                .MenuItems.Add(menuItem2)
    
                ' .Items.Add("Clear")
                'menuItem1.MenuItems.Add("Exit")
    
                'menuItem2.MenuItems.Add("Networking")
                'menuItem2.MenuItems.Add("Programming")
                'menuItem2.MenuItems.Add("Web")
            End With
    
            With menuItem1
                .MenuItems.Add("Clear")
                .MenuItems.Add("Exit")
            End With
    
            With menuItem2
                .MenuItems.Add("Networking")
                .MenuItems.Add("Programming")
                .MenuItems.Add("Web")
            End With
    Code:
     Private Sub menuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem1.Click 'i think this is not correct either, maybe need to handle the click differently?
             
            label.Text = menuItem1.Text
    'this is where i want to basically put a switch to know which 'item i have selected in the menu to do different actions but do 'not know how to retrieve the individual menu items, such as on menuItem1 how do get the clear item click event?
    
        End Sub
    Thanks in advance for any help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    I tried to duplicate what you are doing, but ran into a problem with the first step. When I dimension mainMenu1 as New MainMenu, it is looking for a reference to MainMenu.

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    44
    Rep Power
    0
    ok i figured out what i was doing wrong, i need to add menuitems for each menuitem and then add an event handler for the click.

    Please let me know if that makes sense.

    Thanks.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    I am still not sure what you are trying to do, but I assume that you want to programmatically add items to a menu. This is usually accomplished using an object array. In the example below, I allow the user to enter a new mailbox name. The first level menu is preloaded, and the first menu item of second level is preloaded into a menu array. The routine then copies the structure of a table to a new table using the new name. A new member of the object array is loaded, and the new name assigned to the caption. The new name and it's index is then stored in the Registry to be loaded automatically the next time the program is loaded. Each menu item has a common name and an associated index. There is a common click event containing the index value:
    Private Sub mnuBox_Click(index As Integer)

    J.A. Coutts
    Code:
    Private Sub mnuNew_Click()
        Dim N%
        Dim strTemp As String
        strTemp = InputBox("Enter Name of Mailbox:")
        If Len(strTemp) = 0 Then
            Exit Sub
        Else
            'Check if table already exists
            On Error GoTo Continue
            adoConn1.Execute ("SELECT * FROM " & strTemp & " WHERE 0 = 1;")
            MsgBox strTemp & " already exists!", vbExclamation
            Exit Sub
        End If
    Continue: 'Table does not exist yet
        On Error GoTo mnuNewErr
        adoConn1.Execute ("SELECT * INTO " & strTemp & " FROM Dummy WHERE 0 = 1;")
        'If we get this far, the table was sucessfully created
        'Add to menu
        N% = mnuBox.Count
        Load mnuBox(N%)
        mnuBox(N%).Caption = strTemp
        'Add to registry
        Call SaveSettings("Mailbox" & CStr(N%), strTemp)
        Exit Sub
    mnuNewErr:
        MsgBox "Error " & CStr(err) & ": " & Error$(err)
    End Sub

IMN logo majestic logo threadwatch logo seochat tools logo