ultraman Posted January 9, 2003 Posted January 9, 2003 Hi there ! I'm totally new to VB.NET, but I works with VB6 for a few years now. So there's a lot of adjustment to do ;-) My first question concerns the use of a single ContextMenu for differents controls. How can I retreive the name of the control that called the popup ? I tryed with the System.Object or the System.EventArgs passed as argument in the MyMenu_Click() event, but I can't make it work ? I could declare a flag variable that would be set before the ContextMenu.Show method is called, so I can now who was the caler, but there must be an easier way ? Thanx in advance ! Ultraman Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
*Gurus* divil Posted January 9, 2003 *Gurus* Posted January 9, 2003 There doesn't appear to be a way to do this using the arguments passed to the Popup event of the context menu, so I guess you'll have to set a flag. I guess MS didn't think of people wanting to use the same context menu on different controls. I guess on some controls, you could check their .Focused property and that might be a way to tell if the user just clicked on them. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
ultraman Posted January 9, 2003 Author Posted January 9, 2003 Ok, thanx a lot Divil ! I used the flag for a test and it works just fine, I think I'll use this solution. Ultraman Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
Libertarian Posted August 26, 2003 Posted August 26, 2003 Sorry to resurrect an old thread, but I found it in searching for solutions to the same problem. There really are instances where multiple callers to a context menu is convenient � for example, if you have a list of customer names and you want to right-click a node to perform context-sensitive operations on the data in that particular node. The ContextMenu class has a method called "SourceControl" that identifies the control that launched the context menu. You can then use the object that SourceControl returns to respond to the appropriate caller, something like this: txtPath.Text = DirectCast(cxtMenu.SourceControl, TreeNode).FullPath Quote
mhsueh001 Posted October 2, 2003 Posted October 2, 2003 Question Regarding Libertarian's Method I followed your example and must use sender.parent.sourcecontrol to get this to work. However I must turn Option Strict Off. Is there any way to use the same context menu for multiple controls and leave option strict on? Quote
*Experts* Volte Posted October 2, 2003 *Experts* Posted October 2, 2003 If you use DirectCast to cast Sender.Parent to a ContextMenu object, it should work. Quote
mhsueh001 Posted October 3, 2003 Posted October 3, 2003 (edited) Hi Voltface Thanks for the help, but it still doesn't seem to work. I have to turn Option Strict Off. If I leave it on it says that It can't do late binding with option strict on. I was using Ctype, is DirectCast better? Oh, I did try DirectCast, but Sender.Parent is still underlined in blue with that error of last binding not allowed with option strict on. I'm attaching the context menu to a label which is an e-mail address. If the label is right clicked, it takes the mouse click event and calls ContactEmailClickHandler. So I've got this line in a sub after displaying the label: AddHandler lblEmail.MouseUp, AddressOf ContactEmailClickHandler Public Sub ContactEmailClickHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = MouseButtons.Left Then 'Sends the email address to the email system MessageBox.Show("You have clicked the Email " & CType(ContEmails.Item(CType(CType(sender, System.Windows.Forms.Label).Tag, Integer)).ContactEmailKey, String)) ElseIf e.Button = MouseButtons.Right Then 'Display a context menu Dim cMenu As ContextMenu Dim Menus(2) As MenuItem Dim intKey As Integer Menus(0) = New MenuItem("&New", AddressOf AddNewEmail) Menus(1) = New MenuItem("&Edit", AddressOf EditEmail) Menus(2) = New MenuItem("&Delete", AddressOf DeleteEmail) cMenu = New ContextMenu(Menus) cMenu.Show(CType(sender, Label), New Point(e.X, e.Y)) End If End Sub Private Sub AddNewEmail(ByVal Sender As System.Object, ByVal e As System.EventArgs) Dim l As Label l = DirectCast(Sender.Parent, ContextMenu).SourceControl '***** I STILL GET Sender.Parent underlined saying option strict must be off ***** End Sub [/Code] Edited October 3, 2003 by mhsueh001 Quote
*Gurus* divil Posted October 6, 2003 *Gurus* Posted October 6, 2003 You're casting the wrong thing. You want to cast sender, not sender.Parent. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
mhsueh001 Posted October 6, 2003 Posted October 6, 2003 To: divil Hi Divil, That doesn't work! Sender is of type MenuItem Sender.Parent is of type ContextMenu Sender.Parent.SourceControl is the label that has the information that I want. The label has the .text property which contains the e-mail address. It also has a .tag property that stores the Primary key of the SQL table. So if Sender = Edit, I want to be able to grab the Sender.Parent.SourceControl.Tag so I can edit the appropriate item in the email table using the primary key. Sender = Delete, I can grab the Sender.Parent.SourceControl.Tag and delete the item using the primary key. Maybe my coding is wrong. Like I said the only way I can get it to run is by turning Option Strict Off. Any help is much appreciated. :confused: Quote
Leaders dynamic_sysop Posted October 6, 2003 Leaders Posted October 6, 2003 try changing this bit slightly... Private Sub AddNewEmail(ByVal Sender As System.Object, ByVal e As System.EventArgs) Dim l As Label l = DirectCast(Sender.Parent, ContextMenu).SourceControl to something like this... Private Sub AddNewEmail(ByVal Sender As System.Object, ByVal e As System.EventArgs) Dim mnu As MenuItem = DirectCast(Sender,MenuItem) Dim l As Label = DirectCast(mnu.Parent.SourceControl,Label) hope it helps. Quote
mhsueh001 Posted October 6, 2003 Posted October 6, 2003 Re: dynamic_sysop Hi, that didn't exactly solve the problem either, but you got me on the right track and I got it to work! Thanks. :D From your suggestion: This line works, and it casts sender to a menu item. The parent Dim mnu As MenuItem = DirectCast(Sender,MenuItem) However, this line has mnu.Parent.SourceControl underlined saying that SourceControl is not a member of 'System.Windows.Forms.Menu' Dim l As Label = DirectCast(mnu.Parent.SourceControl,Label) So for those of you referencing this thread in the future. I needed to do this and I could keep option strict on: 'This line casts as a Menu Item Dim mnu As MenuItem = DirectCast(Sender,MenuItem) 'This casts to a Context Menu. Dim cMnu As ContextMenu = DirectCast(mnu.Parent, ContextMenu) 'This casts to the original label! Dim l As Label = DirectCast(cMnu.SourceControl, Label) Thanks to all those that responded!! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.