检查numerc输入vb.net / windows表单应用程序的多个复选框

时间:2015-03-04 09:35:39

标签: vb.net validation textbox numeric

我有一个包含30个(当前)文本框的表单,用户将在其中输入数据。我需要将用户限制为仅在每个文本框中输入数字,最好是在将焦点转移到下一个文本框之前。

感谢互联网,我找到了一段代码,用于单个盒子。我希望尽可能避免重复此代码30次。

我看到许多答案都提到错误提供程序,但我并不清楚如何将其合并到我的代码中。

有人可以怜悯我并向我展示实现目标的简单方法吗?我的头脑正在从我读过的所有帖子中转过来,几乎但没有完全回答我的问题。

谢谢你的期待

史蒂夫

3 个答案:

答案 0 :(得分:0)

以下代码将TextBox的输入限制为数字字符,箭头键,后退,清除和制表符,以及“ - ”和(恰好一个)小数点。

为避免重复代码30次,您需要做的就是添加另一个类似于Private Sub TextBox1_Enter()的子类,其中您将TextBox1的所有出现替换为其中一个TextBox的名称,直到您有一个这样的子类为每个TextBox。

Public WithEvents NumericTextBox As MSForms.TextBox

Private Sub TextBox1_Enter()
    Set NumericTextBox = TextBox1
End Sub

Private Sub NumericTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case KeyAscii
    Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyLeft, _
         vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab 

    Case Asc("-")
      If Instr(1, NumericTextBox.Text, "-") > 0 Or NumericTextBox.SelStart > 0 Then
        KeyAscii = 0
      End If

    Case Asc(".")
      If InStr(1, NumericTextBox.Text, ".") > 0 Then
        KeyAscii = 0
      End If

    Case Else
      KeyAscii = 0
  End Select
End Sub

答案 1 :(得分:0)

您可以使用以下链接

Accept only integer value

    Public WithEvents textbox As MSForms.TextBox

Private Sub TextBox1_Enter()
    Set NumericTextBox = TextBox1
End Sub

Private Sub textbox _KeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs
)
  If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
            e.Handled = True
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            e.Handled = False
    End If
End Sub

答案 2 :(得分:0)

Something I wrote today. Copy and paste it into a new class file, and be sure to import InputVerification where it's used.

Public Class InputVerification
Private ctrlArray As Control()
Private fltArray As String()
Private retMsg As String()

Public Property Case_Insensitive As Boolean

Public Const Yes As Boolean = True
Public Const No As Boolean = False

Public Sub New()
    Case_Insensitive = True
End Sub
''' <summary>
''' Returns a value if InputFilter returns False.
''' </summary>
''' <value>Nothing.</value>
''' <returns>The offending control name and character, in a String array.</returns>

Public Property Return_Message As String()
    Get
        Return retMsg
    End Get
    Set(ByVal value As String())
        retMsg = value
    End Set
End Property

''' <summary>
''' The controls to be checked for valid input.
''' </summary>
''' <value>Any control type with a .Text property.</value>
''' <returns>An array with all the controls specified.</returns>

Public Property Controls() As Control()
    Get
        Return ctrlArray
    End Get
    Set(ByVal value As Control())
        ctrlArray = value
    End Set
End Property

''' <summary>
''' The characters to be allowed.
''' </summary>
''' <value>Any ASCII value.</value>
''' <returns>A String array for each control specified.</returns>

Public Property Filter() As String()
    Get
        Return fltArray
    End Get
    Set(ByVal value As String())
        fltArray = value
    End Set
End Property

''' <summary>
''' Checks to see if the characters entered match the valid characters.
''' Usage: {1, "a-d", 3, "0-5", "e-g", "?"}
''' The number defines how many filters for that control, in order.
''' </summary>
''' <returns>A boolean value.</returns>
''' <remarks>Use Return_Message, if this fails. First a number to define
''' the number of filters for that control. The next are as many filters
''' as you defined.</remarks>

Public Function InputFilter() As Boolean
    Dim i As Integer = 0
    Dim numFilters As Integer = 0
    Dim oldPosition As Integer = 0
    Dim newPosition As Integer = 0
    Dim convValidChars As New Collection
    For Each cntrl As Control In Controls
        convValidChars.Clear()
        oldPosition = newPosition
        numFilters = Filter(newPosition)
        newPosition = newPosition + numFilters
        For k As Integer = oldPosition To newPosition
            If Filter(k).ToString.Contains("-") And Filter(k).ToString.Length > 1 Then
                Dim split() As String = Filter(k).ToString.Split("-"c)
                Dim lowest, highest As Integer
                If AscW(split(0).ToString) > AscW(split(1).ToString) Then
                    lowest = AscW(split(1))
                    highest = AscW(split(0))
                Else
                    lowest = AscW(split(0))
                    highest = AscW(split(1))
                End If
                For j As Integer = lowest To highest
                    convValidChars.Add(j)
                Next

            Else
                convValidChars.Add(AscW(Filter(k)))
            End If
        Next
        Dim matchFound As Boolean = False
        For Each indvUCC In cntrl.Text
            For Each indvVC In convValidChars
                matchFound = False
                Dim indvUCCINT As Integer = Asc(indvUCC)
                If indvVC = indvUCCINT Then
                    matchFound = True
                    Exit For
                Else
                    If Case_Insensitive = True Then
                        If indvUCCINT >= 65 AndAlso indvUCCINT <= 90 Then
                            indvUCCINT = Asc(indvUCC.ToString.ToLower)
                        ElseIf indvUCCINT >= 97 AndAlso indvUCCINT <= 122 Then
                            indvUCCINT = Asc(indvUCC.ToString.ToUpper)
                        End If
                        If indvVC = indvUCCINT Then
                            matchFound = True
                            Exit For
                        End If
                    End If
                End If
            Next
            If matchFound = False Then
                Dim txtbox = TryCast(cntrl, TextBox)
                If txtbox IsNot Nothing Then
                    txtbox.Focus()
                    txtbox.SelectAll()
                End If
                Return_Message = {cntrl.Name, indvUCC}
                Return False
            End If
        Next
        newPosition += 1
    Next
    Return True
End Function
End Class

Example usage:

    Imports System.Windows.Forms
Imports Form1.InputVerification

    Public Class test
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim filter_Test As New InputVerification
            filter_Test.Controls = {TextBox1, TextBox2}
            filter_Test.Filter = {2, "a-z", "0-9", 1, "0-9"}
            filter_Test.Case_Insensitive = Yes
            If filter_Test.InputFilter() = False Then
                Dim respCTRL As String = filter_Test.Return_Message(0)
                Dim respChar As String = filter_Test.Return_Message(1)
                MsgBox("Control " & respCTRL & " has an invalid character, which is " & respChar)
            End If
        End Sub
    End Class

I have it far better written up here, as it's a bit confusing to use at first.

InputVerification Thread

的文字