RichTextBox branch settings Style

Source: Internet
Author: User

RichTextBox is more powerful than common Textbox, because it can contain plain-style data and can be used to set the desired font style and color in segments and branches. It seems simple. During the writing process today, many detours have taken place. The detailed questions that I don't usually pay attention to are displayed. After a long time, I finally ended up writing them for your reference, avoid detours.

The biggest problem encountered was that after the color and font of the string selected by the Branch were set, the data was added and all of them were changed to the set style. Step by step, find the root cause of the problem: line breaks and the method for adding strings.

Manually add an envirement. newline: This line break is counted in the textlength attribute of RichTextBox, which occupies a length, but is not counted in the lines attribute, because the line break is automatically placed in the next line, add "×××" + envirement. after newline, the number of rows increases by 2, while the second row does not.

In addition, the first parameter of the select method of RichTextBox is to select the starting position of the text, which starts from 0 and should not be considered as starting from 1, otherwise, it is easy to select one more row and select the next row. This will result in all selected rows.

As for the method of adding text, we strongly recommend using the append method, because at first I used the + = connection, and found that after selecting a row and adding the text, selectionstart is always 0, think too. If all the text is deleted and then added, is the starting position 0. Therefore, in order to select a location for the backward shift, use the append method to add text. In this way, selectionstart is automatically added, except that the last selected text end location is recorded with variables.

The following is a simple example:

 1:   private void button1_Click(object sender, EventArgs e)
 2:  {
 3:    int selectStart = 0;
 4:   
 5:    richTextBox1.AppendText("??????????"+Environment.NewLine);
 6:    selectStart = richTextBox1.TextLength;
 7:    richTextBox1.Select(selectStart, richTextBox1.TextLength - 1);
 8:    richTextBox1.SelectionColor = Color.Red;
 9:    selectStart = richTextBox1.TextLength;
10:    richTextBox1.AppendText("??????????" + Environment.NewLine);
11:    richTextBox1.Select(selectStart, richTextBox1.TextLength - 1);
12:    richTextBox1.SelectionColor = Color.Blue; ;
13:    selectStart = richTextBox1.TextLength;
14:    richTextBox1.AppendText("??????????" + Environment.NewLine);
15:    richTextBox1.Select(selectStart, richTextBox1.TextLength - 1);
16:    richTextBox1.SelectionColor = Color.Green;
17:  }

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.