PC Review


Reply
Thread Tools Rate Thread

Accessing Sub Menu Items

 
 
shoosh
Guest
Posts: n/a
 
      8th Apr 2010
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.







 
Reply With Quote
 
 
 
 
Phill W.
Guest
Posts: n/a
 
      8th Apr 2010
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.
>
>
>
>
>
>
>


 
Reply With Quote
 
shoosh
Guest
Posts: n/a
 
      9th Apr 2010
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?


 
Reply With Quote
 
shoosh
Guest
Posts: n/a
 
      9th Apr 2010

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?


 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      9th Apr 2010
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
 
Reply With Quote
 
shoosh
Guest
Posts: n/a
 
      9th Apr 2010

That's it.

Thank you, thank you, and thank you.


"Armin Zingler" wrote:

> 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
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unwanted Menu Items in Start Menu and Explore Popup Menu Possum Windows XP General 2 25th Oct 2008 06:40 PM
Removing Menu Items and Child Menu Items Larry Bud Microsoft ASP .NET 13 7th Jan 2008 02:12 PM
Disabled menu items present their submenus (and even allow execution of submenu items) hmoeller.privat@gmail.com Microsoft Dot NET Framework Forms 2 24th May 2007 03:23 AM
To restore menu items of context menu in IE after adding custom items vicsmith@yandex.ru Windows XP Internet Explorer 0 26th Sep 2005 03:52 AM
To restore menu items of context menu in IE after adding custom items vicsmith@yandex.ru Windows XP Internet Explorer 0 21st Sep 2005 09:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:47 PM.