Context Menu determine which control was Right-Clicked

bortiquai

Member
Joined
Jul 5, 2007
Messages
17
Programming Experience
Beginner
I have a form with several multiline Text Boxes. I would like to add a right click function that adds the current date at the current position, where the user might be typing.

However, as soon as you right click and trigger the contextmenuitem.Click event, the textbox no longer has focus. How can i determine which textbox had focus, and where the cursor position was, so that I can add the date at that spot?
Thanks
 
ContextMenuStrip has the SourceControl property for this.
VB.NET:
Private Sub InsertDateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles InsertDateToolStripMenuItem.Click
    Dim tb As TextBox = ContextMenuStrip1.SourceControl
    tb.SelectedText = Date.Now.ToShortDateString
End Sub
 

Latest posts

Back
Top