Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting the Clicked Object from a context menu
Message
From
24/08/2005 05:17:35
 
 
To
All
General information
Forum:
ASP.NET
Category:
Menus
Title:
Getting the Clicked Object from a context menu
Miscellaneous
Thread ID:
01043212
Message ID:
01043212
Views:
52
Hi everyone. I'm new to VB.net and I'm trying so hard to get this simple code to work.

My problem lies at the event handler of the menuitem. The messagebox at the event handler correctly displays the selected menuitem's text from the contextmenu, but the second messagebox in the Button1_Click sub routine doesn't display it correctly.

But if I uncomment the MsgBox("delay") statement the code works well.
What am I doing wrong.

Any help will be very much appreciated

--------------------------------------------------------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form


Private selected As String = ""
Private cm As New ContextMenu

#Region " Windows Form Designer generated code "
#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cm.MenuItems.Add("Text1", AddressOf mnuHandler)
cm.MenuItems.Add("Text2", AddressOf mnuHandler)
cm.MenuItems.Add("Text3", AddressOf mnuHandler)
cm.MenuItems.Add("Text4", AddressOf mnuHandler)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

selected = "INIT"

cm.Show(TextBox1, New Point(0, 0))

'IF THE LINE BELOW IS UNCOMMENTED, THE CODE WORKS
'MsgBox("delay")

'must display the clicked menuitem's text
MsgBox(selected)
End Sub

Private Sub mnuHandler(ByVal sender As Object, ByVal e As System.EventArgs)
selected = CType(sender, MenuItem).Text

'this displays the correct selected menuitem's text
MsgBox(selected + " inside")
End Sub
End Class
--------------------------------------------------------------------------------------------
OMAR C. IBRAHIM
----------
Programmer
STUDENT RECORDS & INFORMATION SYSTEM (SRIS)
Western Mindanao State University, Philippines
----------
Programmer
SSI Engineering & Architectural Designs and IT Center (SENARD ITC)
Zamboanga City, Philippines
Reply
Map
View

Click here to load this message in the networking platform