2017-10-20 1 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の色を使用しますが、追加の文字の場合はエラー

+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

ありがとう、それはすでに実行されていた –

関連する問題