October 17th, 2012, 02:35 AM
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:
Dim mainMenu1 As New MainMenu
Dim WithEvents menuItem1 As New MenuItem
Dim WithEvents menuItem2 As New MenuItem
'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
Thanks in advance for any help.
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?
October 17th, 2012, 02:19 PM
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.
October 17th, 2012, 02:37 PM
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.
October 18th, 2012, 01:06 AM
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)
Private Sub mnuNew_Click()
Dim strTemp As String
strTemp = InputBox("Enter Name of Mailbox:")
If Len(strTemp) = 0 Then
'Check if table already exists
On Error GoTo Continue
adoConn1.Execute ("SELECT * FROM " & strTemp & " WHERE 0 = 1;")
MsgBox strTemp & " already exists!", vbExclamation
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
mnuBox(N%).Caption = strTemp
'Add to registry
Call SaveSettings("Mailbox" & CStr(N%), strTemp)
MsgBox "Error " & CStr(err) & ": " & Error$(err)