|
-
Sep 28th, 2017, 07:19 AM
#1
Thread Starter
Lively Member
[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.
-
Sep 28th, 2017, 02:23 PM
#2
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)
-
Sep 29th, 2017, 08:07 AM
#3
Thread Starter
Lively Member
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()
-
Sep 29th, 2017, 02:45 PM
#4
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!
-
Sep 30th, 2017, 11:53 AM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|