【问题标题】:allow numbers, dots and backspace and delete in textbox允许数字、点和退格并在文本框中删除
【发布时间】:2012-06-28 21:57:29
【问题描述】:

我有以下代码:

Private Sub TxtPStof_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPStof.KeyPress
    e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".")

End Sub

只允许数字和 .在我的文本框中,但是我还需要能够使用退格键或删除按钮删除值。

这可能吗?

谢谢! :)

【问题讨论】:

    标签: vb.net textbox


    【解决方案1】:

    这是错误的方法。

    人们普遍认为,限制用户输入不利于用户体验,并且总是无法处理特殊情况(例如 Ctrl+V 呢? ?啊,你忘了。每个人都这样做)。

    相反,.NET 提供Validating event 来验证用户输入。你应该拦截那个事件,不是按键。 允许用户输入他们想要的文本;特别是,让他们不间断地犯错误(例如错误输入)——这将极具破坏性并且没有有帮助。

    然后,一旦完成(因为输入焦点离开了控件),一次性进行输入验证。

    【讨论】:

      【解决方案2】:

      虽然我完全同意康拉德鲁道夫的回答
      (在 KeyPress 事件中处理用户输入真的很麻烦)
      我想回答你的问题。

      KeyPress 文档中的 MSDN 声明 The KeyPress event is not raised by noncharacter keys。这意味着您没有得到 Delete 键,而只有 BackSpace 键。你可以通过对事件处理程序的这个小改动来处理这种情况

      Private Sub TxtPStof_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPStof.KeyPress 
      
          if e.KeyChar <> ControlChars.Back then
              e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".") 
          end if
      
      End Sub 
      

      【讨论】:

      • 谢谢史蒂夫,我知道它有点乱,但这是为我做这件事的最快方法,而且该程序不会被多人使用,真的只有一个人,所以它不是对我来说问题,即使很难,也很混乱。稍后当我有更多时间时,我将研究验证事件。感谢大家的帮助
      【解决方案3】:
      If Char.IsLetterOrDigit(e.KeyChar) Or e.KeyChar = ControlChars.Back Or e.KeyChar = "." Then
      
              e.Handled = False
      
      Else
      
              e.Handled = True
      
      End If
      

      【讨论】:

      • 这是代码异味:If x Then variable = False Else variable = True 应重写为 variable = Not x
      【解决方案4】:

      这段代码对我来说很好。

       If Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 46 Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 127) Then
              e.KeyChar = ""
              e.Handled = False
          End If
      

      【讨论】:

        【解决方案5】:

        试试这个:

            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                If Not (Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46) Then e.Handled = True
            End If
        

        【讨论】:

          【解决方案6】:
          Private Sub textbox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textbox.KeyPress
              If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
                  e.Handled = True
          
              End If
          End Sub
          

          这应该做你想做的事。

          【讨论】:

          • 你能补充一些解释吗,为什么要使用这个代码?
          【解决方案7】:
          Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
                  Dim NumString = "0123456789.-"
                  If Not NumString.Contains(e.KeyChar) Then
                      If Asc(e.KeyChar) <> 8 Then  'BackSpace is allowed
                          e.KeyChar = ""
                      End If
                  Else
                      If e.KeyChar = "-" Then
                          If Len(Trim(TxtDcrAmt.Text)) > 0 Then
                              e.KeyChar = ""
                          End If
                      End If
          
                      If e.KeyChar = "." Then
                          If TxtDcrAmt.Text.Contains(".") Then
                              e.KeyChar = ""
                          End If
                      End If
          
                      If TxtDcrAmt.Text.Contains(".") Then
                          Dim TLen As Integer = 0
                          TLen = TxtDcrAmt.Text.IndexOf(".") + 2
                          If Len(Trim(TxtDcrAmt.Text)) > TLen Then
                              e.KeyChar = ""
                          End If
                      End If
                  End If
              End Sub
          

          【讨论】:

          • 一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决问题的原因,答案会更有帮助。
          【解决方案8】:

          把它放在按键事件上。这仅允许点和数字

          If Asc(e.KeyChar) <> 8 Then
                      If Asc(e.KeyChar) < 46 Or Asc(e.KeyChar) > 57 Or (Asc(e.KeyChar) < 48 And Asc(e.KeyChar) > 46) Then
                          e.Handled = True
                End If
          End If
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-04-03
            • 1970-01-01
            • 2015-05-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-19
            相关资源
            最近更新 更多