2012-06-12 83 views
0

我有一個函數,應該輸出colred文本RichTextBox。所有的匹配應該是紅色的,而不匹配的文本是黑色的。下面的函數來試圖更改爲被插入我的條目(如Color different parts of a RichTextBox string工作)RichTextBox的內容色彩色輸出到richtextbox不工作

public void OutputColoredMatches(String InputText, MatchCollection Matches, RichTextBox OutputBox) 
{ 
    int LastMatchEndIndex = OutputBox.TextLength; 
    foreach (Match CurrentMatch in Matches) 
    { 
     OutputBox.SelectionColor = Color.Black; 
     OutputBox.AppendText(InputText.Substring(LastMatchEndIndex, CurrentMatch.Index - LastMatchEndIndex)); 
     OutputBox.SelectionColor = Color.Red; 
     OutputBox.AppendText(InputText.Substring(CurrentMatch.Index, CurrentMatch.Length)); 
     LastMatchEndIndex = CurrentMatch.Index + CurrentMatch.Length; 
    } 
    OutputBox.SelectionColor = Color.Black; 
    OutputBox.Text += InputText.Substring(LastMatchEndIndex, InputText.Length - LastMatchEndIndex); 
} 

功能增加之後,應該是黑色文本只設置選擇的顏色爲黑色後,和並且只有在選擇顏色設置爲紅色後才添加找到的匹配文本。儘管單步執行代碼並正確觀察插入文本的所有輸出都是黑色的。

我也嘗試更改插入文本的全部(或部分),然後更改RichTextBox選擇的大小。然後設置選擇顏色,這也不起作用。儘管我檢查並仔細檢查了選區是在適當位置開始和結束,但所有文字最後都是紅色或黑色。 (我嘗試了類似於:Selectively coloring text in RichTextBox)。這裏是插入文本部分的函數的另一個變體,然後改變它的顏色。我也通過這個在調試器踩到並驗證它被選擇的項目,如我所料,然後設置它們的顏色,所有的輸出被黑:

public void OutputColoredMatches(String InputText, MatchCollection Matches, RichTextBox OutputBox) 
{ 
    int SelPos = 0; 
    int LastMatchEndIndex = OutputBox.TextLength; 
    foreach (Match CurrentMatch in Matches) 
    { 
     SelPos = OutputBox.TextLength; 
     OutputBox.AppendText(InputText.Substring(LastMatchEndIndex, CurrentMatch.Index - LastMatchEndIndex)); 
     OutputBox.SelectionStart = SelPos; 
     OutputBox.SelectionLength = OutputBox.TextLength - SelPos; 
     OutputBox.SelectionColor = Color.Black; 

     SelPos = OutputBox.TextLength;    
     OutputBox.AppendText(InputText.Substring(CurrentMatch.Index, CurrentMatch.Length)); 
     OutputBox.SelectionStart = SelPos; 
     OutputBox.SelectionLength = OutputBox.TextLength - SelPos; 
     OutputBox.SelectionColor = Color.Red; 

     LastMatchEndIndex = CurrentMatch.Index + CurrentMatch.Length; 
    } 
    OutputBox.SelectionColor = Color.Black; 
    OutputBox.Text += InputText.Substring(LastMatchEndIndex, InputText.Length - LastMatchEndIndex); 
} 

更具體地講,如果我有一個正則表達式的' ,以及'asdf'的輸入文本,這個函數在輸出框中插入'a'。然後它將選擇位置設置爲0,並將選擇長度設置爲1,然後將顏色設置爲黑色。然後插入's',選擇位置爲1,長度爲1,顏色爲紅色。然後插入'df'將選擇位置設置爲2,長度設置爲2,顏色設置爲黑色。然後所有的輸出都是黑色的。

我也嘗試了各種東西,選擇起始位置和長度,然後插入文本沒有任何影響。我認爲這很可能是我做了一些不正確的事情,只是模糊地與文本框相關。

還有什麼可以影響着色行爲,我可能沒有注意到。

回答

0

你沒有選擇任何顏色,在你給的鏈接中,他們在設置顏色之前定義了一個選擇。

box.SelectionStart = jx; 
box.SelectionLength = phrase.Length; 
box.SelectionColor = color; 

您將需要找到並選擇匹配,然後爲它們着色。

編輯:我還沒有測試這一點,但它看起來大約右..

//set the start of the selection to the current end of the text box 
OutputBox.SelectionStart = OutputBox.Text.Length - 1; 
//set the selections length 
OutputBox.SelectionLength = CurrentMatch.Length; 
//and finally set the color 
OutputBox.SelectionColor = Color.Red; 
+0

感謝您的答覆。根據MSDN(http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectioncolor.aspx)和我提供的其他鏈接插入文本也尊重選擇顏色。我已經嘗試過,但我會再試一次併發布該功能。我認爲,正在發生一些切線事件,就像文本框的某些屬性或特徵在我不知情的情況下會影響它的着色一樣。 – Sqeaky

+0

我發佈了選擇文本的函數的第二個版本,然後設置它的顏色。輸出全黑。 – Sqeaky