这个问题是关于扩展控制的:这里的 FastColoredTextbox:http: //www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting
我正在尝试自动向下滚动文本并将文本光标放在附加文本的最后一个字符中(我想是文本属性的总长度)。
文字滚动问题
这适用于自动文本滚动:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FastColoredTextBox1.Text = str
FastColoredTextBox1.ScrollLeft()
FastColoredTextBox1.Navigate(FastColoredTextBox1.Lines.Count - 1)
End Sub
...但我想让它更通用,这不起作用:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FastColoredTextBox1.Text = str
End Sub
Private Sub FastColoredTextBox1_TextChanged(sender As Object, e As FastColoredTextBoxNS.TextChangedEventArgs) Handles FastColoredTextBox1.TextChanged
sender.ScrollLeft()
sender.Navigate(FastColoredTextBox1.Lines.Count - 1)
End Sub
任何错误或异常,都不会起作用,文本不会像第一个示例中那样滚动。
键盘光标位置问题
关于文本光标,我已经尝试过了,但也不起作用:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FastColoredTextBox1.Text = str
FastColoredTextBox1.SelectionStart = FastColoredTextBox1.Text.Length
End Sub
任何错误或异常,都不起作用。
更新
尝试了@ebyrob 解决方案但不起作用,它不会向下滚动。
Imports FastColoredTextBoxNS
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str As String = String.Empty
For x As Int32 = 1 To 1000 : str += vbNewLine & x : Next
FastColoredTextBox1.Text = str
End Sub
Private Sub FastColoredTextBox1_TextChanged(sender As Object, e As TextChangedEventArgs) Handles FastColoredTextBox1.TextChanged
GoEnd()
End Sub
Public Sub GoEnd()
If FastColoredTextBox1.Lines.Count > 0 Then
FastColoredTextBox1.Selection.Start = New Place(FastColoredTextBox1.Lines(FastColoredTextBox1.Lines.Count - 1).Count, FastColoredTextBox1.Lines.Count - 1)
Else
FastColoredTextBox1.Selection.Start = New Place(0, 0)
End If
FastColoredTextBox1.DoCaretVisible()
End Sub
End Class
更新二
@ebyrob 代码仅在我始终在附加文本后调用“GoEnd”时才有效,如下例所示:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str As String = String.Empty
For x As Int32 = 1 To 1000 : str += vbNewLine & x : Next
FastColoredTextBox1.Text = str
GoEnd()
End Sub
这与我评论过的第一个问题相同(TextChanged 事件未按预期工作),我需要使其高效且通用,我认为 Control 的“TextChanged”事件将是正确的事件放“GoEnd()”,但绝对不是。
我不想这样做:
1. Add text
2. Call GoEnd
3. Add more text
4. Call again GoEnd
我不想将“GoEnd”放在更改文本以简化事情时成功的事件中:
1. add text
2. add more text
更新三
所有问题都解决了,只是更改了事件名称:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mytext As String = String.Empty
For x As Int32 = 1 To 1000 : mytext += vbNewLine & x : Next
FastColoredTextBox1.Text += mytext
FastColoredTextBox1.Focus()
End Sub
Private Sub FastColoredTextBox1_TextChanged(sender As Object, e As FastColoredTextBoxNS.TextChangedEventArgs) _
Handles FastColoredTextBox1.TextChangedDelayed
sender.ScrollLeft()
sender.Navigate(sender.Lines.Count - 1)
FastColoredTextBox1.SelectionStart = FastColoredTextBox1.Text.Length
End Sub
End Class