0

This may be more of a RANT than question. That said the ContextMenuStrip is the worst control I have ever used..
Here is the process I added a ContextMenuStrip (CMS) to a RichTextBox then used a mouse click to show the menu.
It takes two clicks to get the menu to show? WHY is the first part of how to use this control.
Next after selecting the text and clicking COPY I navigate to the same form and RTB to do a PASTE.
NO TEXT on the clipboard? What am I doing wrong?
Here is the code and other code that works but does not use the ContextMenuStrip.

 Public Sub CMS()
    Dim contextMenu As ContextMenuStrip = New System.Windows.Forms.ContextMenuStrip()
    Dim menuItem As New ToolStripMenuItem("Cut")
    contextMenu.Items.Add(menuItem)
    menuItem = New ToolStripMenuItem("Copy")
    contextMenu.Items.Add(menuItem)
    menuItem = New ToolStripMenuItem("Paste")
    contextMenu.Items.Add(menuItem)
    Me.ContextMenuStrip1 = contextMenu
End Sub

Private Sub rtbNotes_MouseClick(sender As Object, e As EventArgs) Handles rtbNotes.MouseClick
    'ContextMenuStrip1.Show(Me.rtbNotes.Location)'This show the menu 2 inches away from the form?
    rtbNotes.SelectAll()
    ContextMenuStrip1.Show(Me, Me.PointToClient(MousePosition))
    CMS()
End Sub

Here is code that works it does not use the CMS.

 Private Sub rtbNotes_Click(sender As Object, e As EventArgs) Handles rtbNotes.Click
    If gvActionType = "View" Then
        Clipboard.SetText(rtbNotes.Text)
        MsgBox("If Fire")
    End If
    If gvActionType = "Add" Then
        If Clipboard.ContainsText(TextDataFormat.Text) Then
            rtbNotes.SelectedText = Clipboard.GetData(DataFormats.Text).ToString()
        End If
    End If

End Sub

OK here is an EDIT
I commented out the SUB CMS and added this code to the MouseClick.
And added to the Context Menu Strip Menu Items COPY and Paste.

   If gvActionType = "View" Then
        Clipboard.SetText(rtbNotes.Text)

    End If
    If gvActionType = "Add" Then
        If Clipboard.ContainsText(TextDataFormat.Text) Then
            rtbNotes.SelectedText = Clipboard.GetData(DataFormats.Text).ToString()
        End If
    End If
Vector
  • 3,066
  • 5
  • 27
  • 54
  • 3
    Since this ContextMenuStrip is dedicated to a RichTextBox, why don't you just design the CMS in the Form Designer and assign it to ContextMenuStrip property of your RTB? -- If you want to show a CMS *manually*, do it in the `MouseUp` event. For sure not `Click` or `MouseClick`. -- Of course you can hide / show / add MenuItems to a CMS at run-time, if some contextual feature is required. Or build the CMS at run-time. But, as mentioned, only when this is required or has a specific reason (e.g., its MenuItems are not known at design-time). – Jimi Mar 07 '22 at 00:31
  • @Jimi I am trying to understand the control as of now the next issue is trying to use COPY SELECTED and not use SelectAll It shows in the mouse click event for now without the SUB CMS My code from another project Copy Selected works but it is behind a button and NO changing forms. Will keep playing Thanks – Vector Mar 07 '22 at 00:38
  • 3
    As described, just assign the CMS to your RTB in the designer (i.e., you don't need the `CMS()` method at all), right-click inside a selection and select the `Copy` MenuItem, where you execute the `[RichTextBox].Copy()` command (or `Paste()` or `Cut()` or whatever else). That's all. – Jimi Mar 07 '22 at 00:41
  • @Jimi Got it working I still think the ContextMenuStrip SUCKS perhaps I am being harsh OR too munch of a Button Clicker ha ha Don't think it works all that great I'll use my alternative code perhaps Thanks – Vector Mar 07 '22 at 00:54

0 Answers0