テキストボックス内の文字列を無効にすることはできませんが、その文字列内でユーザーが変更できないようにすることはできます。たとえば、ユーザーが文字を追加しようとした場合、その文字列内の変更を削除または変更した場合、その文字列が発生しないようにします。文字列のみを表示することができるたかったように、 "1 X 40 HQ STC" を削除する
Private Sub TextBox1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
'see if any change is made between the first 13 characters (13 characters because "1 X 40 HQ STC" had 13 chars)
If sender.selectionstart <= 14 Then
'see if a character is deleted (tried to press backspace or delete key)
If e.KeyCode = 8 Or e.KeyCode = 46 Then
'replace the first 12 characters with our orignal text
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text.Substring(0, 12), "1 X 40 HQ STC")
'if left or right key is pressed
ElseIf e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then
'do nothing
ElseIf e.KeyCode = Keys.Enter Then
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text.Substring(0, 15), "1 X 40 HQ STC")
Else
'it means other text is added, remove 14 characters and put our original string
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text.Substring(0, 14), "1 X 40 HQ STC")
End If
'set the cursor to 15th character, i.e. next line
sender.selectionstart = 15
End If
End Sub
このソリューションは、クライアント側の目的に合っているようです(https://stackoverflow.com/a/21982192)。テキストエリアの 'keydown'イベントや' keypress'イベントを参照し、 'event.keyCode'を使って、キーボードで押したバックスペース/削除ボタンがあるか調べることができます。 –
@ TetsuyaYamamoto tqと私は試してみます –
私の答えをチェックしてください – Subaz