这是我的问题。在KeyUp中
If Textbox1.Text.Length = 3 Then
Textbox1.Text = Textbox1.Text.Insert(3, "-")
Textbox1.SelectionStart = Textbox1.TextLength
End If
输出就像这样:
AAA-0123
例如,我在前3个字母输入错误,我想要改变它,但我仍然无法删除连字符
AAA-
答案 0 :(得分:0)
也许您正在寻找MaskedTextBox
?因此,您只能以此格式输入文字。
Private Sub MaskedTextBox1_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
If (Me.MaskedTextBox1.MaskFull) Then
ToolTip1.ToolTipTitle = "Input Rejected - Too Much Data"
ToolTip1.Show("You cannot enter any more data into the date field. Delete some characters in order to insert more data.", Me.MaskedTextBox1, Me.MaskedTextBox1.Location.X, Me.MaskedTextBox1.Location.Y, 5000)
ElseIf (e.Position = Me.MaskedTextBox1.Mask.Length) Then
ToolTip1.ToolTipTitle = "Input Rejected - End of Field"
ToolTip1.Show("You cannot add extra characters to the end of this date field.", Me.MaskedTextBox1, 0, -20, 5000)
Else
ToolTip1.ToolTipTitle = "Input Rejected"
ToolTip1.Show("You can only add numeric characters (0-9) into this date field.", Me.MaskedTextBox1, 0, -20, 5000)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ToolTip1.IsBalloon = True
Me.MaskedTextBox1.Mask = "00/00/0000"
End Sub
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MaskedTextBox1.KeyDown
' The balloon tip is visible for five seconds; if the user types any data before it disappears, collapse it ourselves.
Me.ToolTip1.Hide(Me.MaskedTextBox1)
End Sub
答案 1 :(得分:0)
这取决于您编辑字符串的方式。无论如何,这正是你想要的:
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If TextBox1.Text.Length = 3 And e.KeyCode <> Keys.Back Then
TextBox1.Text = TextBox1.Text.Insert(3, "-")
TextBox1.SelectionStart = TextBox1.TextLength
End If
End Sub