简体   繁体   中英

make the cursor to the end of textbox/richtextbox

I am creating auto typing application. how its work: • Click on load text file (file will be loaded to richtextbox2) • Click Start: (Timer will start to type the code on richtextbox1 from richtextbox2) • But here I get stuck with problem:

(I used the following code already but don't work for me) used code:

RichTextBox1.SelectionStart = RichTextBox1.TextLength
RichTextBox1.ScrollToCaret()

But Timer Value is 100 and it works with the count_ code: So the Scrollbar Continuously Go ▲▼▲▼▲▼ (up, down, up down, ...) if I removed this code: Then the scrollbar doesn't go down automatically. If manually done then I get on the first line,1st word automatically if timer is in process...

So please Help me what Can I do

将以下内容添加到您的代码中

RichTextBox1.HideSelection = False

To prevent the RichTextbox rebound effect ▲▼ when vertical bar it at bottom you can paste the Class below into your project and use it like in this:

 RichTextBox1.Select(RichTextBox1.TextLength - 1, 1)

 If Not ScrollBarInfo.IsAtBottom(RichTextBox1) Then
    RichTextBox1.ScrollToCaret()
 End If

This is my modified version from code provided here: How to know if RichTextBox vertical Scrollbar reached the max value? of @ King King

#Region " Scrollbar Info "

Public Class ScrollBarInfo

    <System.Runtime.InteropServices.DllImport("user32")> _
    Private Shared Function GetScrollInfo(hwnd As IntPtr, nBar As Integer, ByRef scrollInfo As SCROLLINFO) As Integer
    End Function

    Private Shared scrollInf As New SCROLLINFO

    Private Structure SCROLLINFO
        Public cbSize As Integer
        Public fMask As Integer
        Public min As Integer
        Public max As Integer
        Public nPage As Integer
        Public nPos As Integer
        Public nTrackPos As Integer
    End Structure

    Private Shared Sub Get_ScrollInfo(control As Control)
        scrollInf = New SCROLLINFO()
        scrollInf.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(scrollInf)
        scrollInf.fMask = &H10 Or &H1 Or &H2 'SIF_RANGE = &H1, SIF_PAGE= &H2, SIF_TRACKPOS = &H10
        GetScrollInfo(control.Handle, 1, scrollInf)
    End Sub

    ' IsAtBottom
    Public Shared Function IsAtBottom(control As Control) As Boolean
        Get_ScrollInfo(control)
        Return scrollInf.max = (scrollInf.nTrackPos + scrollInf.nPage) - 1
    End Function

    ' IsAtTop
    Public Shared Function IsAtTop(control As Control) As Boolean
        Get_ScrollInfo(control)
        Return scrollInf.nTrackPos = 0
    End Function

    ' ReachedBottom
    Public Shared Function ReachedBottom(control As Control) As Boolean
        Get_ScrollInfo(control)
        Return scrollInf.max = scrollInf.nTrackPos + scrollInf.nPage
    End Function

    ' ReachedTop
    Public Shared Function ReachedTop(control As Control) As Boolean
        Get_ScrollInfo(control)
        Return scrollInf.nTrackPos < 0
    End Function

End Class

#End Region

When you set the RichTextBox's " Hide Selection " Property to False in the designer, AND use the " AppendText " method, the RichTextBox will automatically scroll down to the bottom line when it is appended.

rtblog. AppendText (dbcon.insertdata & Chr(13))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM