2012-06-22 118 views
0
Public Sub textcolorchanged() 
    Dim searchword As String = RichTextBox2.Text.ToString.Trim 

    Dim index1 As Integer = 0 

    While index1 <> -1 
     If (index1 < ORGFILETXT.Text.Length) Then 
     index1 = ORGFILETXT.Find(searchword, index1, RichTextBoxFinds.None) 
     'If (index1 <= ORGFILETXT.TextLength) Then 
      If index1 <> -1 Then 
       ORGFILETXT.SelectionStart = index1 
       ORGFILETXT.SelectionLength = searchword.Length 
       ORGFILETXT.SelectionColor = Color.Red 
       index1 = index1 + searchword.Length 
      End If 
     'End If 

     Else 
      index1 = -1 
     End if 
    End While 
End Sub 

我搜索了datagridview1行的單詞,並在富文本框中突出顯示搜索詞。文本(它是總文本) 我在datagridview1中將此方法稱爲鼠標單擊和鍵盤按鍵事件richtextbox中的高亮顯示顏色

單詞在富文本框中突出顯示,當我用鼠標單擊並鍵入並按下鍵時datagridview1行的搜索詞,但有時得到全文更改顏色如何? 請幫幫我

回答

1

閱讀RichTextBox.Find Method (String, Int32, RichTextBoxFinds)的文檔我懷疑,如果找不到字符串,返回值是負值,但不是-1。
如果是這種情況,那麼它可能是你的代碼中設置選擇的顏色失敗。

你可以嘗試改變這一行

If index1 <> -1 Then 

If index1 >= 0 Then 

也看到了他們的example in MSDN