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.