Automatic scroll of RichTextBox

G

Guest

Hi,

Is it possible to scroll a RichTextBox programmatically to a given text
selection in it, such that the selected text becomes viewable.

regards.
Jesper.
 
M

Mona

Hi Jesper,

For this, first specify the "SelectedText" of the richtextbox control and
then call the ScrollToCaret method. Here's an example:

'Handles the Enter key being pressed while TextBox1 has focus.
Private Sub TextBox1_KeyDown(ByVal sender As Object, _
ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
TextBox1.HideSelection = False
If e.KeyCode = Keys.Enter Then
e.Handled = True

' Copy the text from TextBox1 to RichTextBox1, add a CRLF after
' the copied text, and keep the caret in view.
RichTextBox1.SelectedText = TextBox1.Text + _
Microsoft.VisualBasic.vbCrLf
RichTextBox1.ScrollToCaret()
End If
End Sub

Hope this helps.
Thanks
Mona [GrapeCity]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top