3

この質問は拡張コントロールに関するものです: 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

アップデート 2

@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()」を入れますが、決定的にそうではありません。

私はこれを作りたくない:

1. Add text
2. Call GoEnd
3. Add more text
4. Call again GoEnd

物事を単純化するためにテキストが変更されたときに成功したイベントに「GoEnd」を入れたくありません。

1. add text
2. add more text

アップデート3

イベント名を変更しただけで、すべての問題が解決されました。

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
4

1 に答える 1

2

どうやら、この操作のためだけのメソッドが既に存在するようです:

    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 ミリ秒の遅延を意味します。

于 2013-05-31T13:51:40.797 に答える