This is a bit of an old-school method, I know, and I'll probably get bagged out for it, but you could create a public Control Object and populate it with a reference to the specific text box in it's MouseDown event. Here's an example of what I mean:
Public MyCont As Control
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown If e.Button = MouseButtons.Right Then MyCont = TextBox1 End If End Sub
Private Sub TextBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown If e.Button = MouseButtons.Right Then MyCont = TextBox2 End If End Sub
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click MsgBox("You opened this menu off of " & MyCont.Name) End Sub
Or you could simply use the form's ActiveControl property:
Private Sub FooToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FooToolStripMenuItem.Click If ActiveControl IsNot Nothing Then MsgBox(ActiveControl.Name) End If End Sub
I have only just checked my email. By strange coincidence I have already solved my own problem exactly how you suggested!
I have used:
if Me.ActiveControl.Name = "TextBox1" then 'copy, paste etc elseif Me.ActiveControl.Name - "TextBox2" then 'copy, paste etc
I use a similar one of these for each item on the context menu. I have also handled the mousedown event of each textbox, and if the right button was pressed I use some simply logic to set the enabled state of each menu item.
There are numerous ways to do this but here is one way, which is fairly simple and straightforward.
To hook up a context menu to several controls.
1. Drag a context menu strip control on the form.
2. Establish the menu items you want and code in there click event code.
3. For all the controls that you want to use this context menu strip - set the contextmenustrip property of the controls (ie. the textboxes)
4. That will make the single context menu appear for each of these controls.
Determine which textbox the context menu was called from
The next thing you'll need to do - to determine which is the active textbox which this context menu was used from is to add some code similar to the following (this example has two textboxes and two context menu items)
Public Class Form1 Dim CurrentTextbox As TextBox 'This is a variable used to reference to the current textbox
Private Sub TestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem.Click 'Look at the currentTextbox control Name If CurrentTextbox IsNot Nothing Then MsgBox(CurrentTextbox.Name.ToString) End If End Sub
Private Sub ABCToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ABCToolStripMenuItem.Click 'Look at the currentTextbox control Name If CurrentTextbox IsNot Nothing Then MsgBox(CurrentTextbox.Name.ToString) End If End Sub
''' <summary> ''' Sets a reference Variable CurrentTextBox to the Active Textbox control which fired this event ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus CurrentTextbox = sender End Sub End Class
What is happening is that when one of the textboxes gets the focus it will set a reference CurrentTextbox. When you click on a menu item it will look at this variable to get the details of the last textbox entered.
You may have to play around with the code a little for you own purposes and perhaps set CurrentTextbox = nothing for other controls on the form but the general principle is there.
1 context menu for several controls
Skyruner2
Thats true I forgot about that one....
Chris Breaux
Public MyCont As Control
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
If e.Button = MouseButtons.Right Then
MyCont = TextBox1
End If
End Sub
Private Sub TextBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown
If e.Button = MouseButtons.Right Then
MyCont = TextBox2
End If
End Sub
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
MsgBox("You opened this menu off of " & MyCont.Name)
End Sub
Hope this helps you out, dude. Good luck with it!
PareshPatel
Or you could simply use the form's ActiveControl property:
Private Sub FooToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FooToolStripMenuItem.Click
If ActiveControl IsNot Nothing Then
MsgBox(ActiveControl.Name)
End If
End Sub
Rafael Nami
I have used:
if Me.ActiveControl.Name = "TextBox1" then
'copy, paste etc
elseif Me.ActiveControl.Name - "TextBox2" then
'copy, paste etc
I use a similar one of these for each item on the context menu.
I have also handled the mousedown event of each textbox, and if the right button was pressed I use some simply logic to set the enabled state of each menu item.
Thankyou
BQMartin
There are numerous ways to do this but here is one way, which is fairly simple and straightforward.
To hook up a context menu to several controls.
1. Drag a context menu strip control on the form.
2. Establish the menu items you want and code in there click event code.
3. For all the controls that you want to use this context menu strip - set the contextmenustrip property of the controls (ie. the textboxes)
4. That will make the single context menu appear for each of these controls.
Determine which textbox the context menu was called from
The next thing you'll need to do - to determine which is the active textbox which this context menu was used from is to add some code similar to the following (this example has two textboxes and two context menu items)
Public Class Form1
Dim CurrentTextbox As TextBox 'This is a variable used to reference to the current textbox
Private Sub TestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem.Click
'Look at the currentTextbox control Name
If CurrentTextbox IsNot Nothing Then
MsgBox(CurrentTextbox.Name.ToString)
End If
End Sub
Private Sub ABCToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ABCToolStripMenuItem.Click
'Look at the currentTextbox control Name
If CurrentTextbox IsNot Nothing Then
MsgBox(CurrentTextbox.Name.ToString)
End If
End Sub
''' <summary>
''' Sets a reference Variable CurrentTextBox to the Active Textbox control which fired this event
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus
CurrentTextbox = sender
End Sub
End Class
What is happening is that when one of the textboxes gets the focus it will set a reference CurrentTextbox. When you click on a menu item it will look at this variable to get the details of the last textbox entered.
You may have to play around with the code a little for you own purposes and perhaps set CurrentTextbox = nothing for other controls on the form but the general principle is there.