Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ContextMenu in RichTextBox 1

Status
Not open for further replies.

shalbert

Programmer
Sep 9, 2008
3
0
0
When someone "right clicks" a richTextBox and a contextMenu is opened up, I would like to dynamicly load the contextMenu based upon what text was located on the line that they right clicked on. For example...

If you have a richTextBox that contains these 3 lines...

Hi there!
How are you?
I'm fine!

.... and you "right click" over the line that says "How are you?", how do I obtain that line so that I can show it in my contextMenu when it opens?

I've been thinking of ways to do this, but non come to mine. Any idea or suggestions would be greatly appreciated.
 
I can't remember off the top of my head, but try a search here this has come up twice in the last 6 months.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
you are going to need to figure out which line was clicked on in the rich text box.

Then you are going to need to get the text from that line.

After that you are going to have to add the item to the contextmenu

(sooner or later, you are going to have to remove the old line item. This will be easier if you have this as the first or last item.)

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Sorwen: I've seen a lot of similar issues (like about how to add copy/paste functionality to a ContextMenu for rtb) but nothing like what I'm trying to do.

Yes Qik3Coder: That's exactly what I need to do, and my problem is how to make that happen? How do I get the line from which I right click? That really would solve my problem I guess.
 
I'm sure there's a simpler solution, but here's something to get you started/thinking.

Since a right-click does not set the SelectionStart property, this code simulates a left-click in the rich text box whenever a right-click is detected. It then resets the original selection so as not to disturb the text box's original state.
Code:
    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
 
You'll also need to include the following Imports:
Code:
Imports System.Runtime.InteropServices
 
DaveInIowa - This is what I knew I needed to find out - how to make a right click actually position the cursor. From there it's easy. My application works great now! Thanks!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top