数值和连字符-&引用;inputbox vb.net

数值和连字符-&引用;inputbox vb.net,vb.net,handle,Vb.net,Handle,如何接受输入框上的连字符和数值 我有 Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles inputBox_1.KeyDown Select Case e.KeyCode Case Keys.D0 To Keys.D9, Keys.NumPad0 To Keys.NumPad9, _

如何接受输入框上的连字符和数值

我有

Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles inputBox_1.KeyDown
        Select Case e.KeyCode
            Case Keys.D0 To Keys.D9, Keys.NumPad0 To Keys.NumPad9, _
                 Keys.Back, Keys.Delete, Keys.Left, Keys.Right
                If e.Shift = True Then
                    e.SuppressKeyPress = True
                    Exit Sub
                End If
                e.SuppressKeyPress = False
            Case Else
                e.SuppressKeyPress = True
        End Select
    End Sub
但在行中输入“-”字符时失败

Keys.D0 To Keys.D9, Keys.NumPad0 To Keys.NumPad9, _
                     Keys.Back, Keys.Delete, Keys.Left, Keys.Right

如何编写连字符?

最好创建自定义控件,并在需要数字文本框的任何地方使用它 此外,对于位数限制,我认为textbox有一个maxlength属性 下面的链接可以回答你的问题吗


这里有一个解决方案。这比我希望的要长一点,您可以通过将textbox text属性拆分为单个字符并全部选中来加快它的速度(但是如果您希望编写代码将字母放入textbox,那么这样做就可以了)

无论如何,我测试了这个,它工作正常,它在秒表上显示为0毫秒,所以它不应该给用户提供任何明显的减速。注意,有一个全局布尔变量

    Dim boo As Boolean = True

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    Dim temp As String
    Dim tempchr As Integer
    If boo = True Then
        boo = False

        temp = Mid(TextBox1.Text, TextBox1.SelectionStart, 1)
        tempchr = Asc(temp)

        If tempchr < 48 Or tempchr > 58 Then
            If tempchr = 45 Then

            Else
                If TextBox1.SelectionStart - 1 > 0 Then
                    If TextBox1.SelectionStart = TextBox1.TextLength Then
                        TextBox1.Text = Mid(TextBox1.Text, 1, TextBox1.SelectionStart - 1)
                    Else
                        TextBox1.Text = Mid(TextBox1.Text, 1, TextBox1.SelectionStart - 1) & Mid(TextBox1.Text, TextBox1.SelectionStart + 1)
                    End If

                Else
                    If TextBox1.SelectionStart = TextBox1.TextLength Then
                        TextBox1.Text = ""
                    Else
                        TextBox1.Text = Mid(TextBox1.Text, TextBox1.SelectionStart + 1)
                    End If

                End If

            End If
        Else
            boo = True
        End If

    Else
        boo = True
    End If
    TextBox1.SelectionStart = TextBox1.TextLength
End Sub
Dim boo As Boolean=True
私有子TextBox1_TextChanged(发送方作为System.Object,e作为System.EventArgs)处理TextBox1.TextChanged
作为字符串的Dim temp
Dim tempchr作为整数
如果boo=True,则
boo=False
temp=Mid(TextBox1.Text,TextBox1.SelectionStart,1)
tempchr=Asc(温度)
如果tempchr<48或tempchr>58,则
如果tempchr=45,则
其他的
如果TextBox1.SelectionStart-1>0,则
如果TextBox1.SelectionStart=TextBox1.TextLength,则
TextBox1.Text=Mid(TextBox1.Text,1,TextBox1.SelectionStart-1)
其他的
TextBox1.Text=Mid(TextBox1.Text,1,TextBox1.SelectionStart-1)和Mid(TextBox1.Text,TextBox1.SelectionStart+1)
如果结束
其他的
如果TextBox1.SelectionStart=TextBox1.TextLength,则
TextBox1.Text=“”
其他的
TextBox1.Text=Mid(TextBox1.Text,TextBox1.SelectionStart+1)
如果结束
如果结束
如果结束
其他的
boo=True
如果结束
其他的
boo=True
如果结束
TextBox1.SelectionStart=TextBox1.TextLength
端接头

您真的想阻止他们在框中键入这些值吗?还是让他们在框中键入,然后让验证对超出范围的值大惊小怪?我想我在这里真正想要的是围绕您的问题提供更多的上下文,以便我们能够找出最适合您实际需要的解决方案。我想这个问题会更好,我想也许我应该在用户填写表单的时候进行验证,你知道如何使用我在所有文本框中的代码吗?你可以在
Handles
之后给出一个提示。当你只需要处理基本控件的验证时,真的不需要费劲地创建自定义控件。另外,我认为
MaxLength
不适合限制数字的大小,因为需要支持负数