Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

A Custom Context Menu for Selected Text (in a TextBox) And Only For Selected Text?

$
0
0

I want to have a custom context menu for selected text in a TextBox.  So, if text is selected, then when a right mouse click occurs within the selected text I want my context menu to appear.  If the right click occurs outside of the selected text then nothing should happen. 

The problem I am having is that my code is not seeing the right mouse click (left mouse clicks it does see).  And since it does not see the right mouse click it cannot know whether the right mouse click ocurred within the selected text. 

The relevant code is below.  "cmsSelected" is the ContextMenuStrip.

Private Sub HtmlForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        tbxHtml.ContextMenuStrip = cmsSelected

    End Sub

    Private Sub tbxHtml_MouseClick(sender As Object, e As MouseEventArgs) Handles tbxHtml.MouseClick



        If e.Button = Windows.Forms.MouseButtons.Right Then
            
            tbxHtmlWhereClick = e

        End If

    End Sub

    Private Sub cmsSelected_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles cmsSelected.Opening

        With tbxHtml
            If .SelectionLength = 0 Then
                e.Cancel = True
                Exit Sub
            Else
                Dim CharIndex As Integer = .GetCharIndexFromPosition(New Point(tbxHtmlWhereClick.X, tbxHtmlWhereClick.Y))
                MsgBox("CharIndex: " & CharIndex.ToString & " / SelectionStart: " & .SelectionStart & _" / SelectionLength: " & .SelectionLength.ToString)
                If (CharIndex >= .SelectionStart) And (CharIndex >= .SelectionStart + .SelectionLength) Then
                    MsgBox("bingo")
                End If
            End If
        End With

    End Sub
I'll appreciate any help anyone can offer. Thanks, Bob


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>