我有一個RichTextBox. 我正在嘗試撰寫代碼,以便如果在 的SelectionBackColor屬性中找到特定顏色RTB,則要洗掉的單詞/文本的背景顏色。為此,我需要檢測RTB. 但是,根據
This is when only one color and a whitespace are applied in the RTB(fail),

This is when multiple colors and whitespaces are applied in the RTB(fail),

So, is there any ways to override the return value of the SelectionColor property to be able to detect multiple colors, or are there any other ways of doing this? Just so you know, I've searched for this kind of problem over the internet, but I didn't think I've find any related issues.
uj5u.com熱心網友回復:
我花了一段時間才弄清楚@TaW 在評論部分的意思,但多虧了他,我已經設法解決了這個問題。
實際上,根據我在評論部分中向@TaW 提出的回復,我認為相同的概念實際上是錯誤的。在上面的帖子中,我所做的完全錯誤:
- 我應該一一評估每個文本以了解它們的顏色。但是,下面的代碼一開始就已經錯了:
int startIndex = 0;
int endIndex = this.richTextBox1.TextLength;
this.richTextBox1.Select(startIndex, endIndex);
- 為了分析
RTB.SelectionColor、RTB.SelectionStart和RTB.SelectionLength作業原理,我決定創建另一個專案。該專案是一個簡單的程式,其中包含一個 RTB,以及一些用于管理 RTB 的其他按鈕SelectionColor。如果您想查看我已完成的專案,我們隨時歡迎您訪問
- 如果您并不真正關心評估 中的每個文本
RTB,則應改為如下編碼:
private void methodB(RichTextBox localRTB) { int startIndex; int endIndex; if (localRTB.SelectionLength.Equals(0)) //if user don't select any text { startIndex = 0; //from beginning of RTB endIndex = localRTB.TextLength; //'til the end of RTB //--manage selected text in the RTB-- localRTB.SelectionStart = startIndex; localRTB.SelectionLength = endIndex; localRTB.Select(localRTB.SelectionStart, localRTB.SelectionLength); //--manage selected text in the RTB-- } else if (!(localRTB.SelectionLength.Equals(0))) //if user has text selected { startIndex = localRTB.SelectionStart; //from beginning of RTB endIndex = localRTB.SelectionLength; //'til the end of RTB if (localRTB.SelectedText.Contains(" ")) //skips whitespaces if selected together with text { if (localRTB.SelectedText.EndsWith(" ")) { endIndex -= 1; } } //--manage selected text in the RTB-- localRTB.Select(startIndex, endIndex); //--manage selected text in the RTB-- } }下面顯示了上述代碼的結果(2):

和平...??
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/450326.html - 如果您并不真正關心評估 中的每個文本
