|
-
Feb 24th, 2006, 11:53 AM
#1
Thread Starter
Hyperactive Member
Context menu help [resolved]
Okay I have a couple (well 50 or so) of text boxes on my form which all share the same context menu.
Is there any way to find out which text box the user has right clicked on.
VB Code:
Private Sub MenuItem8_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
MsgBox("you have just right clicked " & ClickedTextBox)
End Sub
To clarify what I'm trying to do, each text box contains a name of a person, when you right click on that person I want to be able to send that person a message, but to do this I need to know what text box has been clicked.
Last edited by Oliver1; Feb 28th, 2006 at 09:50 AM.
-
Feb 24th, 2006, 12:23 PM
#2
Re: Context menu help
i cant remember if you use sender to find out which textbox or if there's a property listed in e but one of the two will tell ya which textbox called the menu
-
Feb 24th, 2006, 12:26 PM
#3
Re: Context menu help
should be the sender parameter that will hold what control that caused the event to fire.
-
Feb 24th, 2006, 12:28 PM
#4
Re: Context menu help
you could do the following...
VB Code:
Private Sub MenuItem8_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
If TypeOf sender Is TextBox Then
MsgBox("you have just right clicked " & CType(sender, TextBox).Name)
End If
End Sub
-
Feb 24th, 2006, 12:44 PM
#5
Frenzied Member
Re: Context menu help
I'm not sure how to do it (still thinking), but in both the suggestions Sender will not do it. Sender is not the text box, but the MenuItem that is being clicked.
-
Feb 24th, 2006, 12:51 PM
#6
Frenzied Member
Re: Context menu help
Ok here is a way to do it. May not be the best, but it works.
You are going to need to create a mousedown event for the textboxes. In the mouse down event save the sender in a variable and then call that variable in menu click.
Example
VB Code:
Private test As String
Private Sub ContextMenuStrip1_Open(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
MessageBox.Show("TextBox clicked was " & test)
End Sub
' notice I only need one procedure for two textboxes because I set the handles to both of them.
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown, TextBox2.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
test = sender.ToString
End If
End Sub
End Class
-
Feb 24th, 2006, 01:09 PM
#7
Re: Context menu help
here i got it for you and i through this together really quick....
VB Code:
Private Sub TestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem.Click
If TypeOf sender Is ToolStripMenuItem Then
If TypeOf CType(sender, ToolStripMenuItem).Owner Is ContextMenuStrip Then
Debug.WriteLine(CType(CType(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl.Name)
End If
End If
End Sub
-
Feb 28th, 2006, 08:17 AM
#8
Thread Starter
Hyperactive Member
Re: Context menu help
Sorry for the delay, got dragged of on other projects.
Have just had a quick look at vbdotnetboy solution and I don't understand and can't get it to work.
What is a ToolStripMenuItem assumed it was a menuitem, but menuitem doesn't have an owner.
Also assumed ContextMenuStrip was a Contextmenu but not sure it is.
Will have a look at mpdeglau soloution now, which looks a lot more understandable, but less elegant (sorry).
Anyway thanks all for you help so far and any more help would be very much appreciated.
-
Feb 28th, 2006, 09:47 AM
#9
Re: Context menu help
 Originally Posted by Oliver1
Sorry for the delay, got dragged of on other projects.
Have just had a quick look at vbdotnetboy solution and I don't understand and can't get it to work.
What is a ToolStripMenuItem assumed it was a menuitem, but menuitem doesn't have an owner.
Also assumed ContextMenuStrip was a Contextmenu but not sure it is.
Will have a look at mpdeglau soloution now, which looks a lot more understandable, but less elegant (sorry).
Anyway thanks all for you help so far and any more help would be very much appreciated.
i'm very, very sorry... i wrote that using vb2005 with the 2.0 framework, and you're obviously not using that.
-
Feb 28th, 2006, 09:49 AM
#10
Thread Starter
Hyperactive Member
Re: Context menu help
No thanks alot for your help, just figured it out
CType(CType(sender, MenuItem).Parent, ContextMenu).SourceControl.Name
which is pretty much the same as you had.
Thanks again, never would have figured it out on my own.
-
Feb 28th, 2006, 09:58 AM
#11
Re: Context menu help [resolved]
glad i could help... i was just getting ready to post that to you too
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|