您好,我正在尝试使用 Visual Studio 使用 VB.Net 制作类似于记事本的东西

问题描述

我有两个richtextBox,richtextBox2 是我写文本的地方,richtextBox1 是行号。我希望我的行号始终与richtextBox2 的行同步。但我面临的问题是,每次我输入文本时,richtextBox1 中的滚动条都会滚动到顶部(如果感觉我输入了错误的行号)。如何阻止这种情况发生,以便每次在richtextBox2 中滚动或键入文本时,richtextBox1 始终指向正确的行号。

我假设我必须在更改文本时在richtextBox1 和richtextBox1.focus() 中将隐藏选择设置为false。请帮我解决这个问题。

以下是我的代码

Const WM_USER As Integer = &H400
Const EM_GETSCROLLPOS As Integer = WM_USER + 221
Const EM_SETSCROLLPOS As Integer = WM_USER + 222
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageW" (ByVal hWnd As IntPtr,ByVal msg As Integer,ByVal wParam As Integer,ByRef lParam As Point) As Integer

Private Sub RTB2_VScroll(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles RichTextBox2.VScroll,RichTextBox1.VScroll

    Dim pt As Point = New Point

    SendMessage(RichTextBox2.Handle,EM_GETSCROLLPOS,pt)

    SendMessage(RichTextBox1.Handle,EM_SETSCROLLPOS,pt)

End Sub

Private Sub RichTextBox2_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles RichTextBox2.TextChanged
    'Dim pt1 As Point = New Point

    'SendMessage(RichTextBox2.Handle,pt1)

    'SendMessage(RichTextBox1.Handle,pt1)
    RichTextBox1.Focus()
    Dim maxlc As Integer = 1
    Dim prevline As Integer = 1
    Dim linecount As Integer = RichTextBox2.GetLineFromCharIndex(RichTextBox2.TextLength) + 1
    If prevline <> linecount Then

        If linecount <> maxlc Then
            RichTextBox1.Clear()
            For i = 0 To linecount - 1 Step 1
                RichTextBox1.AppendText(Convert.ToString(i) + vbNewLine)
            Next i
            maxlc = linecount
            prevline = linecount
        End If

    End If
End Sub

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)