简体   繁体   English

vb.net 2010始终在RichTextBox顶部附加文本

[英]vb.net 2010 append text always at top of a richtextbox

I have create a simple function to append text to a Richtextbox. 我创建了一个简单的函数来将文本追加到Richtextbox。 I want that this text is append always on top scrolling the old text to the bottom. 我希望此文本始终附加在顶部,将旧文本滚动到底部。

Private Sub BoxMessaggio(ByVal testo As String, ByVal errore As Integer)
    Me.ActiveControl = RichTextBox1
    RichTextBox1.Focus()
    If errore Then
        RichTextBox1.SelectionColor = Color.Red
    Else
        RichTextBox1.SelectionColor = Color.Black
    End If
    RichTextBox1.AppendText(testo + vbNewLine)
    RichTextBox1.SelectionStart = RichTextBox1.Text.Length
    'RichTextBox1.Select(RichTextBox1.TextLength, 0)
    RichTextBox1.ScrollToCaret()
End Sub

I call the function in this way: 我以这种方式调用该函数:

BoxMessaggio(Now + ": " + ex.Message, 1)

I tried a lot of different solutions found here on StackOverflow or in some forums but no one works for me, text is always add at the bottom.... 我尝试了在StackOverflow或某些论坛上找到的许多不同解决方案,但是没有人适合我,文本总是添加在底部。

Treat the RichTextBox1.Text as a String (because it is), call Insert , and place the new text at the front of the strong. RichTextBox1.Text视为String (因为它是String ),调用Insert ,然后将新文本放置在文本框的最前面。

RichTextBox1.Text = RichTextBox1.Text.Insert(0, testo + vbNewLine)

As for the scrolling. 至于滚动。 Don't call Focus or ScrollToCaret and it will stay at the top viewing the most recently added text. 不要调用FocusScrollToCaret ,它将停留在顶部,以查看最近添加的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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