Click here to Skip to main content
15,907,328 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am a beginner in VB.NET and this is my first post :)
I have used a context menu strip with some options, which get enabled for textbox1 in my project. But if I have 'n' textboxes, then i want this same custom context menu strip to get enabled to all of them automatically.

My logic to solve this problem was to get the textbox name (out of n textboxes) over which the user right clicks and then transfer the same textbox name as a string to context menu strip code, which would fire the context menu strip for any text box on my form. So is my logic correct?

Any suggestion or any other logic is welcomed. Thanks for reading!
Posted
Comments
Sergey Alexandrovich Kryukov 16-Aug-13 20:33pm    
What do you mean by "name"? Hope, not the property "Name" (bad idea, really)? Maybe the Text? Why?
And you cannot ask anything about UI without tagging the UI library or application type you are using.
—SA

You have to set all TextBox's ContextMenuString property with the ContextMenu. And in the menu item click event, you have to the following.

1. Cast the sender to ToolStripMenuItem
2. Cast its Owner property to ContextMenuStrip
3. Cast SourceControl property of this menuStrip to TextBox. This will be the TextBox control from where the menu invoked.
4. Now you can use the Name/Text properties of this textbox.


VB
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
    Dim text As String = DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl.Text
End Sub


or

VB
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
    Dim selectedItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
    Dim contentMenu As ContextMenuStrip = DirectCast(selectedItem.Owner, ContextMenuStrip)
    Dim sourceTextBox As TextBox = DirectCast(contentMenu.SourceControl, TextBox)
    Dim text As String = sourceTextBox.Text
End Sub
 
Share this answer
 
v4
Comments
Member 10199683 17-Aug-13 12:45pm    
Thanks Sergey and Akbar
To Akbar: can you please elaborate with example, a bit confused.Getting some error message when I debug.
I did a bit of R&D by combining different codes and finally got the code working...
Thanks a lot for the help..
Here's the Code for VB
The prerequisite for this as Akbar already mentioned
1) Enable contextmenustip for every textbox on the form
2) Mention them after Handles
3) Textbox4 is just for confirmation, to identify over which TB you clicked i.e 1,2 or 3
4) Now you can use obj to link any textbox over which you right click


VB
Dim obj As Object
Dim txt As String
Dim current As String

Private Sub RightClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown, TextBox2.MouseDown, TextBox3.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then

            If TypeOf sender Is TextBox Then
                txt = DirectCast(sender, Control).Name
                TextBox4.Text = ("" & txt)

                For Each elem In Me.Controls
                    If elem.Name = txt Then
                        obj = elem
                        current = obj.Text
                        Exit For
                    End If
                Next

            End If

        End If
    End sub
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900