我很難創建能夠拾取特定單詞併爲它們着色的代碼。 我目前使用此代碼:着色基於richtextbox的編程
private void Colorize(string word, Color color, int startIndex)
{
if (this.richTextBox1.Text.Contains(word))
{
int index = -1;
int selectStart = this.richTextBox1.SelectionStart;
while ((index = this.richTextBox1.Text.IndexOf(word, (index + 1))) != -1)
{
this.richTextBox1.Select((index + startIndex), word.Length);
this.richTextBox1.SelectionColor = color;
this.richTextBox1.Select(selectStart, 0);
this.richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);
this.richTextBox1.SelectionColor = Color.Black;
}
this.richTextBox1.SelectionColor = Color.Black;
}
}
的問題是,當RichTextBox的文本太大它掛起,並從頂層運行至底部,有什麼辦法可以即時顏色的關鍵字? 我正在做一個基本的IDE,但我需要一些基於顏色的基於java的關鍵字。
任何錯誤抱歉我使用谷歌翻譯。
是「掛」上色莎士比亞(超過五百萬個字符)的全集意味着它真正掛起或只知道它需要很長時間?您可能想使用'richTextBox1.SupendLayout'和'richTextBox1.ResumeLayout'來防止屏幕更新,直到完成。除此之外,您可以嘗試直接使用正確的字體和顏色表代碼來修改'richTextBox1.Rtf'屬性,但這相當噁心。 – TaW
這聽起來像您想要實現語法高亮?如果是這樣,你應該使用現有的庫/解決方案。看到這個問題的一些選項:http://stackoverflow.com/questions/2809545/how-to-create-syntax-highlighting-text-box。不過,這個問題很老,所以從那時起可能會有更好的選擇。 –
http://stackoverflow.com/a/3282911/17034 –