How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
Go Back  Xtreme Visual Basic Talk > > > How to determine item clicked in dymanically created ContextMenuStrip


Reply
 
Thread Tools Display Modes
  #1  
Old 04-27-2016, 01:23 PM
vbbillm vbbillm is offline
Newcomer
 
Join Date: May 2008
Posts: 2
Default How to determine item clicked in dymanically created ContextMenuStrip


Having a problem determining which item in a dynamically created ContextMenuStrip has been clicked.
The program works like this: when the cursor hovers over Button1 we fill ContextMenuStrip1 with four items, add an event handler for item clicks within that submenu, then display the submenu.
When an item in the submenu is clicked, the SubmenuClick routine is called.
How do I determine which item in the submenu has been clicked?
Running VB 2010
Thanks for your help.
Quote:
Private Sub Button1_MouseHover(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.MouseHover
Dim x, y, x1, y1 As Integer
'calculate position of where to display the ContextMenuStrip
x = sender.Location.X
y = sender.Location.Y
x1 = Me.Location.X + x + 50
y1 = Me.Location.Y + y + 55
'fill the ContextMenuStrip with four items
ContextMenuStrip1.Items.Clear()
ContextMenuStrip1.Items.Add("Item 1")
ContextMenuStrip1.Items.Add("Item 2")
ContextMenuStrip1.Items.Add("Item 3")
ContextMenuStrip1.Items.Add("Item 4")
'set the event handler for submenu clicks
AddHandler ContextMenuStrip1.Click, AddressOf SubmenuClick
'now display the subment
ContextMenuStrip1.Show(x1, y1)
End Sub
Private Sub SubmenuClick(ByVal sender As Object, ByVal e As EventArgs) Handles ContextMenuStrip1.Click
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'HOW DO I DETERMINE WHICH ITEM IN THE CONTEXTMENUSTRIP WAS CLICKED?
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Beep()
End Sub

Last edited by Cerian Knight; 04-28-2016 at 09:27 AM.
Reply With Quote
  #2  
Old 04-28-2016, 08:52 AM
Cerian Knight's Avatar
Cerian KnightHow to determine item clicked in dymanically created ContextMenuStrip Cerian Knight is online now
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,199
Default

Change the handler definition/declaration and make sure you don't add the handler more than once, otherwise you will fire multiple events. My solution to remove the handler before adding it is poor, so maybe just not use AddHandler (change the sub definition like I did). I tried it both ways and it works:
Quote:
Private Sub Button1_MouseHover(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.MouseHover
Dim x, y, x1, y1 As Integer
'calculate position of where to display the ContextMenuStrip
x = sender.Location.X
y = sender.Location.Y
x1 = Me.Location.X + x + 50
y1 = Me.Location.Y + y + 55
'fill the ContextMenuStrip with four items
ContextMenuStrip1.Items.Clear()
ContextMenuStrip1.Items.Add("Item 1")
ContextMenuStrip1.Items.Add("Item 2")
ContextMenuStrip1.Items.Add("Item 3")
ContextMenuStrip1.Items.Add("Item 4")
'set the event handler for submenu clicks
'RemoveHandler ContextMenuStrip1.ItemClicked, AddressOf SubmenuClick
'AddHandler ContextMenuStrip1.ItemClicked, AddressOf SubmenuClick
'now display the subment
ContextMenuStrip1.Show(x1, y1)
End Sub

Private Sub ContextMenuStrip1_Click(sender As Object, e As ToolStripItemClickedEventArgs) Handles ContextMenuStrip1.ItemClicked
Debug.Print(e.ClickedItem.ToString)
Beep()
End Sub
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').

Last edited by Cerian Knight; 04-28-2016 at 08:59 AM.
Reply With Quote
  #3  
Old 04-28-2016, 09:15 AM
vbbillm vbbillm is offline
Newcomer
 
Join Date: May 2008
Posts: 2
Default Thank You

Cerian, thank you so much. I suspected it had to be something pretty simple but I just couldn't figure it out.
Thanks You!!!
Reply With Quote
Reply

Tags
item, contextmenustrip, submenu, clicked, display, determine, object, byval, clicks, handler, submenuclick, private, handles, eventargs, sender, event, fill, contextmenustrip1.click, created, items, subment, position, beep, calculate, submenuclickbyval


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
 
How to determine item clicked in dymanically created ContextMenuStrip
How to determine item clicked in dymanically created ContextMenuStrip
 
-->