C # writes data to the bottom of the RichTextBox control

Source: Internet
Author: User

C # often use the RichTextBox control to display data, so if after writing the data, let RichTextBox always get focus or let RichTextBox always show the latest data at the bottom?

The usual approach is to use focus first to get focused, then Select the method, scroll to the bottom, so inefficient, and write more code:

Richtextbox1.focus ();
Richtextbox1.select (richTextBox1.Text.Length, 0);

Refer to this automatic scrolling to the bottom of the event function:

private void Richtextbox1_textchanged (object sender, EventArgs e)
{//scroll to the bottom
if (RichTextBox1.Lines.Length > 8000)
{
int n = 3000;
int start = richtextbox1.getfirstcharindexfromline (0);//index of the first character in the first line
int end = Richtextbox1.getfirstcharindexfromline (n);//index of first character of Nth line
Richtextbox1.select (start, end);//select N rows
Richtextbox1.selectedtext = "";//Set the contents of the top N rows to be empty
}
Richtextbox1.focus ();
Richtextbox1.select (richTextBox1.Text.Length, 0);
}

In fact there is a simpler way to append data with the Richtextbox.appendtext method, as long as you set the RichTextBox HideSelection property to False.

The Richtextbox.hideselection property is inherited from TextBoxBase:
"Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control lo SES focus. "

When the Richtextbox.hideselection value is
True, the selected text does not appear highlighted when the text box control loses focus;
False, the selected text remains highlighted when the text box control loses focus.
The default is true.

This means that when richtextbox.hideselection is Flase, the RichTextBox control will be highlighted and displayed, regardless of whether RichTextBox gets the focus, which is equivalent to the effect of always having focus, thus realizing Richtextbox.appendtext append data, automatically scroll to the bottom of the effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.