2013-02-22 71 views
1

我試圖使它成爲可以,我可以在代碼中指定一個或多個單詞,並且當我編譯/調試時,我希望程序爲所有人搜索richtextbox這些詞的實例,並改變那裏的顏色。在RichTextBox中更改Word的所有實例的顏色

Dim GRAB as String = New WebClient().DownloadString("example.com") 
RichTextBox1.Text = GRAB 
` Color Word Code Here 

我在谷歌上查了很多東西,但是我試過的所有東西都只會突出顯示第一個詞。

很抱歉,如果我的打字是壞的,即時通訊打字手臂骨折..

有人可以幫助我,或寫一個快速的片段?

+0

您可能對此[代碼項目文章]感興趣(http://www.codeproject.com/Articles/10675/Enabling-syntax-highlighting-in-a-RichTextBox) – AbZy 2013-02-22 23:43:59

回答

0

您需要選擇將有顏色變化的文本。

RichTextBox1.Select(RichTextBox1.Text.IndexOf("example"),4) 
RichTextBox1.SelectionColor = Color.Red 

會呈現紅色 或

RichTextBox1.Select(6,4) 
    RichTextBox1.SelectionColor = Color.Red 

的 「.COM」 會做同樣的

+0

嗯,webclient抓住了源代碼的網頁.. – Ron 2013-02-23 00:10:46

+0

哎呀,我需要放慢速度,閱讀整個問題!如果字符串GRAB是「example.com」,答案纔會有效。 – SubtleStu 2013-02-23 01:19:48

1

試試這個:

Dim wordslist As New List(Of String) 
wordslist.Add("Hello") 
wordslist.Add("World") 

Dim len As Integer = RichTextBox1.TextLength 

For Each word As String In wordslist 

    Dim lastindex = RichTextBox1.Text.LastIndexOf(word) 
    Dim index As Integer = 0 

    While index < lastindex 

    RichTextBox1.Find(word, index, len, RichTextBoxFinds.None) 
    RichTextBox1.SelectionColor = Color.Blue 
    index = RichTextBox1.Text.IndexOf(word, index) + 1 

    End While 

Next 

修改和Here從C#編譯

0

假設ü要做出 '點心',以藍色爲VS: -

粘貼此:

If RichTextBox1.Text.EndsWith("Dim") Then 
     RichTextBox1.Select(RichTextBox1.TextLength - 3, 3) 
     RichTextBox1.SelectionColor = Color.Blue 
     RichTextBox1.Select(RichTextBox1.TextLength, RichTextBox1.TextLength) 
     RichTextBox1.SelectionColor = Color.Black 
    End If 

添加該代碼的RichTextBox到Text_Changed。