2017-10-20 154 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

非常感谢,它已经在运行 –