【问题标题】:VB2010: Allow Float/Integers, Backspace, and Range of Value in TextboxVB2010:在文本框中允许浮点数/整数、退格和值范围
【发布时间】:2013-04-03 08:56:34
【问题描述】:


我在 Visual Basic 中有一个文本框(V​​isual Studio 2010,.net 框架 4.0)
现在我有一个问题!
我希望该用户只输入整数、浮点数、退格键和值范围?
困惑?
哦耶 我希望该用户只输入 0 - 4 之间的值(值可能是十进制的 3.49)
现在我想要完整的代码:
我有这个:
这是有效的,但我无法指定 0-4 之间的范围

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) 处理 TextBox1.KeyPress

        Dim FullStop As Char
        FullStop = "."

        ' if the '.' key was pressed see if there already is a '.' in the string
        ' if so, dont handle the keypress

        If e.KeyChar = FullStop And TextBox1.Text.IndexOf(FullStop) <> -1 Then
            e.Handled = True
            Return
        End If

        ' If the key aint a digit
        If Not Char.IsDigit(e.KeyChar) Then
            ' verify whether special keys were pressed
            ' (i.e. all allowed non digit keys - in this example
            ' only space and the '.' are validated)
            If (e.KeyChar <> FullStop) And
               (e.KeyChar <> Convert.ToChar(Keys.Back)) Then
                ' if its a non-allowed key, dont handle the keypress
                e.Handled = True
                Return
            End If
        End If
End Sub


如果有人给我完整的代码,我会很高兴
提前致谢

【问题讨论】:

    标签: vb.net visual-studio-2010


    【解决方案1】:

    我刚刚用charactersascii codes的Benifits解决了你的问题, 试试这个,如果您对以下实现有任何疑问,请随时给我评论。

     Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    
            'Ascii code 8 for backspace -- Ascii code 46 for (. period)
            If Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
    
                'If typed character is a period then we have to ensure that more than one of it
                'Should not get allowed to type. And also we have to check whether the period symbol
                'may cause any conflicts with MaxNo that is 4
                If Asc(e.KeyChar) = 46 Then
                    If TextBox1.Text.IndexOf(".") <> -1 Or Val(TextBox1.Text.Trim & e.KeyChar) >= 4 Then
                        e.Handled = True
                    Else
                        Exit Sub
                    End If
                Else
                    'If pressed key is backspace, then allow it.
                    Exit Sub
                End If
    
            End If
    
            'Checking whether user typing more than 4 or not.
            If Val(TextBox1.Text.Trim & e.KeyChar) > 4 Then
                e.Handled = True
            End If
    
            '48 - 57  = Ascii codes for numbers
            If (Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57) Then
                e.Handled = True
            End If
    
        End Sub
    

    【讨论】:

    • 哇!!!
      太棒了!
      这真的很有效,非常感谢
      感谢您抽出时间 Jazakallah :)
    • 如果你想告诉我,请告诉我,我从那里学到的 asci 值对于退格是 abd blah blah ...... ……?而且我想提前学习vb.net,我读了大约5本书,但我仍然很弱,所以我的问题是我从哪里学习以及这本书的名字是什么,这将对我有所帮助。我想喜欢你(因为这很难:p)
    • @xXcramaXerXx 看来你对学习一门编程语言很有热情,这种态度会帮助你牢牢抓住你要找的东西。无论如何,不​​要总是依赖书籍,自己做一些项目。如果您遇到困难,请从专家那里获得建议。我敢肯定,它会自动提高你的技能。快乐编码:)
    • 非常感谢您的帮助,我真的很高兴与像您这样的专家交流,是的,我想,我完成了不同的项目,但我在不同的场合仍然犹豫不决,这意味着我我仍然是 nOOb,这就是为什么我要书或从中逐步学习的好地方,再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2015-05-08
    相关资源
    最近更新 更多