シリアルポート経由で通信するプログラムを書いています。送信されるすべてのデータはミラーリングされます。バックスペース以外はすべて正常に動作します。バックスペース ボタンを押したときに、テキスト ボックスの最後の文字を削除する唯一の方法は、mid 関数を使用して現在のデータを新しいデータで上書きすることです。リッチテキストボックス内に大量のデータがあると、ちらつき始めます。richtextbox.text.remove 関数を使用してみましたが、このエラーが発生します。「インデックスとカウントは、文字列内の場所を参照する必要があります。パラメータ名: カウント」
RichTextBox1.Text.Remove(RichTextBox1.TextLength, 1)
エラーが発生しない関数にいくつかの数値をスローしようとしましたが、リッチテキストボックスからデータが削除されません。
ここにデータを送信するコードがあります
KeyCharString = e.KeyChar 'stores key being pressed into KeyCharString
Try
SerialPort1.Write(KeyCharString) 'tx data for key being pressed
Catch ex As Exception
MsgBox(ex.Message) 'Displays error if serialport1 cannot be written to
End Try
If Asc(KeyCharString) = 8 Then 'If char is a backspace remove precious character and exit sub
RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.TextLength, 1)
'RichTextBox1.Text = Mid(RichTextBox1.Text, 1, RichTextBox1.TextLength - 1)'Old code used to remove the character. Causes the richtextbox to flicker when rewriting the data
Exit Sub
End If
これはデータを受け取るコードです
receivedString = SerialPort1.ReadExisting.ToString
If Asc(receivedString) = 8 Then 'deletes the received data if it is a backspace
receivedString = ""
Exit Sub
End If
RichTextBox1.AppendText(receivedString) 'adds new data to the richtextbox
内部のすべてのデータを書き換えずに、リッチテキストボックスから 1 文字を削除する方法はありますか? また、richtextbox は読み取り専用です。