简体   繁体   中英

RichTextBox changing letters color - speed problem

My file is 450 503 letters text. I have to change every letter (eg. 'b' - user choice) on another and mark it by set red color. When I do it in that way:

 for(int i=0; i<lenght; ++i) {
    this.rtb.Select(i, 1);
    this.rtb.SelectionColor = Color.Red;
    this.rtb.SelectedText = this.rtb.SelectedText;
    this.rtb.DeselectAll();
 }

It's too slooow - actually it never finished... (17min awaiting). I have no idea how to speed it up.

It's because you are forcing it to to redraw everytime it happens.

Wrap this in a SuspendLayout and ResumeLayout

Try calling rtb.SuspendLayout(); before running your logic and rtb.ResumeLayout(); afterwards. Like this:

rtb.SuspendLayout();

for(int i=0; i<lenght; ++i) {
    this.rtb.Select(i, 1);
    this.rtb.SelectionColor = Color.Red;
    // you shouldn't need these lines:
    // this.rtb.SelectedText = this.rtb.SelectedText;
    // this.rtb.DeselectAll();
 }

rtb.ResumeLayout();

Optimization aside you do at some point have to check whether the selected letter is the one you want. The current loop will attempt to color every single letter red.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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