0
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

I have question about that code. If I have several textboxes and all textboxes will have different maximum and minimum values, those minimum and maximum values are declared in the module for each textbox, then how can I add these values to that code?

Because that code shows at a moment Min=0 and Max=0 but actually I have different values.

Risto
  • 3
  • 2

2 Answers2

0

Why not use the Validating event for each text box and the error provider.

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
Mary
  • 14,926
  • 3
  • 18
  • 27
0

You can use a Dictionary of Object to Tuple to store the min/max. (You can add more to the tuple if you want for example a custom error message or colors, etc.)

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

* Edited to use pre .NET 7.0 Tuple syntax

djv
  • 15,168
  • 7
  • 48
  • 72
  • Thank you for your feedback! I have there on error with Private ranges As New Dictionary. It says me that "Inigializes anew instance of the Dictionary(Of Key, TValue) class that is empty" "Predefined type 'ValueTuple(Of ,) is not defined or imported. – Risto Dec 11 '18 at 19:45
  • Maybe you have an older version of visual studio or .NET. I will update. – djv Dec 11 '18 at 20:37
  • Thank you very much! That worked exactly like I was thinking. First version didn't work because I use older framework or what version exactly (I just want to know why the first didnt work)? But one quick question, my computer region settings have decimal separator comma but if I add max number for example 10,1 then it is not like number but if I add 10.1 then it shows me that it's bigger then limit? What I should add more that program knows that computer has comma separator? – Risto Dec 11 '18 at 20:57
  • @Risto [Starting with Visual Studio 2017 you get to use named Tuples](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/tuples). – djv Dec 11 '18 at 22:01
  • Double.TryParse will use your computer's regional settings unless you force the thread into another region / language. But I found [a trick](https://stackoverflow.com/a/18571989/832052) which can help you achieve what you want to do. – djv Dec 11 '18 at 22:03
  • Thank you again! I understood that you are expert in that field. I have again one problem. Problem is - I have variable Public WindCurve1.Zmin As Double. I have textbox with name Zmin_1_TB. So I want that if Zmin_1_TB.Textchanged then it will write new value to WindCurve1.Zmin. What I do, I split textbox name to get variable name (Zmin and 1) and puting together with Value="WindCurve"&"."&Str(0)&Str(1) and then would like that it write value=Convert.ToDouble(Zmin_1_TB.Text). But problem is, that the value is string. Do you have any idea? – Risto Dec 15 '18 at 12:02