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 tha开发者_如何学JAVAt 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.
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.
It's because you are forcing it to to redraw everytime it happens.
Wrap this in a SuspendLayout
and ResumeLayout
精彩评论