2011-03-15 43 views
2

我的文件是450 503個字母文本。我必須改變另一個字母(例如'b' - 用戶選擇)並用紅色標記。 當我做那樣:RichTextBox更改字母顏色 - 速度問題

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(); 
} 

這太slooow - 實際上它永遠不會結束......(17分鐘等待)。 我不知道如何加快速度。

+0

現在不記得了,但是沒有像rtb.BeginUpdate和End或ResumeUpdate這樣的一對方法嗎? – 2011-03-15 20:56:11

+0

'this.rtb.SelectedText = this.rtb.SelectedText'有什麼意義? – 2011-03-15 20:57:39

+0

Windows程序員只需在富編輯窗口中觸發['EM_FINDTEXT'](http://msdn.microsoft.com/zh-cn/library/bb788009(v = VS.85).aspx)消息,並按照每個命中與['EM_SETCHARFORMAT'](http://msdn.microsoft.com/en-us/library/bb774230(v = VS.85).aspx)消息。毫無疑問,有一個.net等價物。這將在幾秒鐘內完成。 – 2011-03-15 21:00:24

回答

0

嘗試運行你的邏輯和rtb.ResumeLayout();事後收到主叫rtb.SuspendLayout();。就像這樣:

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(); 

優化拋開你在某些時候要檢查所選字母是否是你想要的。當前循環將嘗試爲每個單獨的字母上色。

0

這是因爲你每次發生它都迫使它重新繪製。

包裝這在SuspendLayoutResumeLayout