Results 1 to 5 of 5

Thread: [RESOLVED] How to make a contextMenuStrip to scroll to a position on a RichTextBox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Resolved [RESOLVED] How to make a contextMenuStrip to scroll to a position on a RichTextBox

    Am trying to make a contextMenuStrip to scroll to a certain position on a RichTextBox on the selected text. When you use a small file on the RichTextBox, it is working perfectly... But when you use a large file, the Menustrip cannot scroll to that position with the help of the scrollbars on the RichTextBox. How can I solve this problem from the code here:

    Where word is selected on:

    Code:
     ContextMenuStrip1.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength))

    Show the Menu under the selected text on the RichTextBox:


    Code:
        Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    
            Try
                With RichTextBox1
                    Dim pt As Point = Me.PointToClient(.PointToScreen(.GetPositionFromCharIndex(.SelectionStart)))
                    pt.Y += .Font.Height ' * Assuming all text use same Font/Height!
                    ContextMenuStrip1.Show(PointToScreen(pt))
                End With
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Exception Window", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
            End Try
            Exit Sub
        End Sub
    Last edited by nqioweryuadfge; Sep 28th, 2017 at 07:23 AM.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to make a contextMenuStrip to scroll to a position on a RichTextBox

    Try this
    Code:
            Dim p As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength)
            If p.X < 0 OrElse p.Y < 0 Then
                RichTextBox1.ScrollToCaret()
            End If
            ContextMenuStrip1.Show(RichTextBox1, p)



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: How to make a contextMenuStrip to scroll to a position on a RichTextBox

    Your code is good, but I found a solution by placing this code:
    Code:
    RichTextBox.Focus()
    on top of the following:

    Code:
    ContextMenuStrip1.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart + RichTextBox1.SelectionLength))
    So, when I tested your code, I was also forced to include that code on top of the
    Code:
    ContextMenuStrip.Show()

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [RESOLVED] How to make a contextMenuStrip to scroll to a position on a RichTextBo

    You have marked this thread as [RESOLVED], but i didn't understood how RichTextBox.Focus() let the RichTextBox scroll to the caret position!



  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: [RESOLVED] How to make a contextMenuStrip to scroll to a position on a RichTextBo

    The contextMenustrip does not appear under the text if the content on the document is large. So, you have to focus it in that section of the scrollbars for it to appear in that position. Caret did not solve my problem.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width