Prev: ASP Tab Control
Next: Gridview order
From: shoosh on 7 Apr 2010 22:04 In VB.NET, I create a File menu. One of the File Menu items is "Recent Projects". Using the following Code, I can add sub-menu items to drop down below "Recent Projects." mnuRecentProjects.DropDownItems.Add("Test Item 4") mnuRecentProjects.DropDownItems.Add("Test Item 5") mnuRecentProjects.DropDownItems.Add("Test Item 6") So now at run time I have 3 sub-menu items off my "Recent Projects" menu item. Question: What event is raised when I click on one of these, say "Test Item #4" and how do I access it? In VB6 it was simple, in vb.net I can't seem to find how to access the underlying code for a sub-menu click event.
From: Phill W. on 8 Apr 2010 08:50 On 08/04/2010 03:04, shoosh wrote: > In VB.NET, I create a File menu. One of the File Menu items is "Recent > Projects". Using the following Code, I can add sub-menu items to drop down > below "Recent Projects." > > mnuRecentProjects.DropDownItems.Add("Test Item 4") > mnuRecentProjects.DropDownItems.Add("Test Item 5") > mnuRecentProjects.DropDownItems.Add("Test Item 6") > > So now at run time I have 3 sub-menu items off my "Recent Projects" menu item. > > Question: What event is raised when I click on one of these, say "Test Item > #4" and how do I access it? It will be a Click event of some sort, depending on the Type of menu control you use (MenuItem or ToolStripMenuItem). If you're creating the menu items manually, use the AddHandler statement to associate a handling routine with that event, something like: Private Sub Form_Load() _ Handles MyBase.Load Dim mi as New MenuItem( "Test Item 3" ) AddHandler mi, AddressOf MenuItem_Click mnuRecentProjects.DropDownItems.Add( mi ) ' IIRC, the ToolStripMenuItem has a constructor to do the ' whole lot in one go. Dim tmi as New ToolStripMenuItem( "Test Item 4" _ , Nothing _ , AddressOf MenuItem_Click ) mnuRecentProjects.DropDownItems.Add( tmi ) End Sub .... then ... Private Sub MenuItem_Click( _ ByVal sender as Object _ , ByVal e as EventArgs _ ) If Not ( typeof sender is MenuItem ) Then Return End If Dim mi as MenuItem = DirectCast( sender, MenuItem ) ' mi now contains a reference to the item clicked End Sub > > In VB6 it was simple, in vb.net I can't seem to find how to access the > underlying code for a sub-menu click event. > > > > > > >
From: shoosh on 8 Apr 2010 21:15 Phil, I can add the sub-menu item, but still can't seem to reach the click event. I've added the following code to a command button: Dim mi As New MenuItem("Test Item 3") AddHandler mi.Click, AddressOf MenuItem_Click mnuRecentProjects.DropDownItems.Add(mi.Text) Then added the following to the form module. Sub MenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) MsgBox("Reached Click Event.") End Sub Clicking on the sub-menu item "Test Item 3" doesn't reach the MenuItem_Click sub. Any ideas?
From: shoosh on 8 Apr 2010 21:59 I think the missing element in my code is that "mi" refers to an object, but the add method simply adds a sub-menu item with the text "Test Item 3". mnuRecentProjects.DropDownItems.Add(mi) generates an error, as the add method doesn't expect an object here. mnuRecentProjects.DropDownItems.Add(mi.Text) works to add the sub-menu item, but this menu item does not equate to the "mi" object. How do I assign the "mi" object to the sub-menu item created with the preceding add method?
From: Armin Zingler on 9 Apr 2010 05:16
Am 09.04.2010 03:59, schrieb shoosh: > > I think the missing element in my code is that "mi" refers to an object, but > the add method simply adds a sub-menu item with the text "Test Item 3". > > mnuRecentProjects.DropDownItems.Add(mi) generates an error, as the add > method doesn't expect an object here. mi.text is a String which is also an Object. Everything is an object. However, the problem is that you use a MenuItem instead of a ToolStripMenuItem. The MenuItem is used with the old menus. You are using the newer MenuStrips. > mnuRecentProjects.DropDownItems.Add(mi.Text) works to add the sub-menu item, > but this menu item does not equate to the "mi" object. > > How do I assign the "mi" object to the sub-menu item created with the > preceding add method? Dim mi As New ToolStripMenuItem("Test Item 3") AddHandler mi.Click, AddressOf MenuItem_Click mnuRecentProjects.DropDownItems.Add(mi) - or - Dim mi As ToolStripItem mi = ItemsToolStripMenuItem.DropDownItems.Add("Test Item 3") AddHandler mi.Click, AddressOf MenuItem_Click - or - mnuRecentProjects.DropDownItems.Add("Test Item 3", Nothing, AddressOf MenuItem_Click) or, or,... -- Armin |