VB.net需要文本框只接受最大和最小数目

时间:2018-12-10 13:54:23

标签: vb.net

Imports System.Drawing
Imports System.Text.RegularExpressions
Imports System.Windows.Forms

Module Module1
    Public Enum ValidationType
        MaxMin = 1
    End Enum
    Public Sub AssignValidation(ByRef CTRL As TextBox, ByVal Validation_Type As ValidationType, Min As Double, Max As Double)
        Dim txt As TextBox = CTRL

        Select Case Validation_Type
            Case ValidationType.MaxMin
                AddHandler txt.TextChanged, AddressOf MaximumMinimum
        End Select

    End Sub

    Public Sub MaximumMinimum(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim NO As TextBox = sender
        If Val(NO.Text) < Min Then
            NO.Focus()
        ElseIf Val(NO.Text) > Max Then
            NO.Focus()
        End If
    End Sub

End Module

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AssignValidation(Me.TextBox1, ValidationType.MaxMin,Zo.Min,Zo.Max)
End Sub

我对该代码有疑问。如果我有多个文本框,并且所有文本框都将具有不同的最大值和最小值,那么这些最小值和最大值将在每个文本框的模块中声明,那么如何将这些值添加到该代码中?

因为该代码会立即显示Min = 0和Max = 0,但实际上我有不同的值。

2 个答案:

答案 0 :(得分:0)

为什么不对每个文本框和错误提供程序使用Validating事件。

Private err As New ErrorProvider()
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    If TextBox1.Text = "" Then
        e.Cancel = True
        err.SetError(TextBox1, "This text box cannot be blank.")
    Else
        err.Clear()
    `enter code here`End If
End Sub

答案 1 :(得分:0)

您可以使用对象字典到元组来存储最小值/最大值。 (如果需要,可以向元组添加更多,例如自定义错误消息或颜色等。)

Option Strict On
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AssignValidation(Me.TextBox1, ValidationType.MaxMin, 5, 10)
        AssignValidation(Me.TextBox2, ValidationType.MaxMin, 0, 5)
    End Sub
End Class
Module Module1
    Private ranges As New Dictionary(Of Object, Tuple(Of Double, Double))()
    Public Enum ValidationType
        MaxMin = 1
    End Enum
    Public Sub AssignValidation(CTRL As TextBox, Validation_Type As ValidationType, Min As Double, Max As Double)
        Select Case Validation_Type
            Case ValidationType.MaxMin
                If Not ranges.ContainsKey(CTRL) Then ranges.Add(CTRL, New Tuple(Of Double, Double)(Min, Max))
                AddHandler CTRL.TextChanged, AddressOf MaximumMinimum
        End Select
    End Sub
    Public Sub MaximumMinimum(sender As Object, e As System.EventArgs)
        Dim textbox = DirectCast(sender, TextBox)
        Dim value As Double
        If Double.TryParse(textbox.Text, value) Then
            ' SUCCESS - parsed as Double
            If value >= ranges(sender).Item1 AndAlso value <= ranges(sender).Item2 Then
                ' SUCCESS - within min and max
            Else
                ' FAIL - outside min or max
                textbox.Focus() ' what does this even do?
            End If
        Else
            ' FAIL - did not parse as Double
            MessageBox.Show(textbox.Text)
        End If
    End Sub
End Module

*编辑为使用.NET 7.0之前的元组语法