2017-10-20 153 views
0
Private Sub Write_code_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Write_code.TextChanged 
     Dim strWord As String 
     Dim lPos As Long 

     strWord = "read" 

     lPos = InStr(1, Write_code.Text, strWord, vbTextCompare) 

     If lPos > 0 Then 
      With Write_code 
       .SelectionStart = lPos - 1 
       .SelectionLength = Len(strWord) 
       .SelectionColor = Color.Green 
       .SelectionStart = Len(Write_code.Text) 
       .SelectionLength = 0 
       .SelectionColor = Color.Blue 
      End With 
     End If 
End Sub 

這是我的代碼,我有一個問題,當我再次打開該文本,它不能添加文字(可以只添加到末尾文本末尾的文字)。有沒有其他的代碼可以幫助我。使用顏色RichTextBox的,但是在CON TRO鼠標錯誤時附加字符的字符

+0

您的標題不能反映您的問題的詳細信息。請更新以避免可能被關閉的問題。 – Codexer

+0

對不起。我已經修復了它 –

回答

0

您正在使用此「.SelectionStart = Len(Write_code.Text)」設置文本末尾的選擇「您可以跟蹤最後一個選擇並將其設置回來。

Private Sub Write_code_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Write_code.TextChanged 
     Dim strWord As String 
     Dim lPos As Long 

     strWord = "read" 

     lPos = InStr(1, Write_code.Text, strWord, vbTextCompare) 

     If lPos > 0 Then 
      Dim originalPosition As Long = Write_code.SelectionStart 

      With Write_code 
       .SelectionStart = lPos - 1 
       .SelectionLength = Len(strWord) 
       .SelectionColor = Color.Green 
       .SelectionStart = Len(Write_code.Text) ' Or maybe put it here 
       .SelectionLength = 0 
       .SelectionColor = Color.Blue 
      End With 

      Write_code.SelectionStart = originalPosition 
     End If 
End Sub 
+0

非常感謝,它已經在運行 –