Private Sub RichTextBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
Static HandlingRightMouseDown As Boolean = False
Static SaveSelectionStart As Integer = 0
Static SaveSelectionLength As Integer = 0
If (HandlingRightMouseDown) Then
HandlingRightMouseDown = False
' RichTextBox1.SelectionStart will give you the position where the user
' right-clicked. From there you can determine what line was clicked.
Me.Text = RichTextBox1.SelectionStart.ToString '<- Put in title for debugging
' Once we've captured the position that was right-clicked, we can now
' restore the original selection ...
RichTextBox1.SelectionStart = SaveSelectionStart
RichTextBox1.SelectionLength = SaveSelectionLength
' Edit the context menu, then pop it up ...
ContextMenuStrip1.Show(RichTextBox1, e.Location)
Exit Sub
End If
If (e.Button = Windows.Forms.MouseButtons.Right) Then
HandlingRightMouseDown = True
SaveSelectionStart = RichTextBox1.SelectionStart
SaveSelectionLength = RichTextBox1.SelectionLength
' Simulate clicking the left button to set the SelectionStart property ...
SendMouseInput(MOUSEEVENTF_LEFTDOWN, e)
SendMouseInput(MOUSEEVENTF_LEFTUP, e)
End If
End Sub
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As MOUSEINPUT, ByVal cbSize As Integer) As Integer
End Function
Const INPUT_MOUSE As Integer = 0
Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Const MOUSEEVENTF_LEFTUP As Integer = &H4
Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
Const MOUSEEVENTF_MOVE As Integer = &H1
Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
Const MOUSEEVENTF_RIGHTUP As Integer = &H10
Private Structure MOUSEINPUT
Public type As Integer
Public dx As Integer
Public dy As Integer
Public mouseData As Integer
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As IntPtr
End Structure
Private Sub SendMouseInput(ByVal MouseButton As Integer, ByVal MouseArgs As MouseEventArgs)
Dim mouseEvent As New MOUSEINPUT
With mouseEvent
.type = INPUT_MOUSE
.dx = MouseArgs.X
.dy = MouseArgs.Y
.mouseData = 0
.dwFlags = MouseButton
.time = 0
'.dwExtraInfo()
End With
Dim result As Integer = SendInput(1, mouseEvent, Marshal.SizeOf(mouseEvent))
End Sub