[visual-studio]FastColoredTextbox 问题滚动文字和定位插入符号

标签: Visual-Studio
发布时间: 2014/3/1 4:00:02
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这个问题是关于扩展控件: 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 事件不按预期方式工作) 相同的问题,我需要使它有效率和泛型,我本来应该"TextChanged"控件的事件的将会是要把"GoEnd()"的正确事件但不是 definitivelly。

不想这过:

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

解决方法 1:

显然已经是只用于此操作的方法:

    public void GoEnd()
    {
        if (lines.Count > 0)
            Selection.Start = new Place(lines[lines.Count - 1].Count,
                                        lines.Count - 1);
        else
            Selection.Start = new Place(0, 0);

        DoCaretVisible();
    }

请注意,您还希望:

Dim tbSender as FastColoredTextBox
tbSender = DirectCast(sender, FastColoredTextBox) 
tbSender.Text = str
tbSender.GoEnd()
' ...

最后我建议:

Option Explicit

在差不多所有的 VB 代码。

编辑: 测试控件后, 延迟事件工程

FastColoredTextBox1.TextChangedDelayed += FastColoredTextBox1_TextChanged;

注意: 这意味着默认 100 ms 延迟发射的每个事件。

官方微信
官方QQ群
31647020