trouble setting a max and min value

I have the following code:

Private Sub numericTextboxKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tb As TextBox = CType(sender, TextBox)
Dim chr As Char = e.KeyChar
If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
'If adding the character to the end of the current TextBox value results in
' a numeric value, go on. Otherwise, set e.Handled to True, and don't let
' the character to be added.
e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
ElseIf Not Char.IsControl(e.KeyChar) Then
'IsControl is checked, because without that, keys like BackSpace couldn't
' work correctly.
e.Handled = True
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
End If
End Sub

This allows only numeric characters to be entered into the textbox. I just cannot figure out a way to limit the maximum value and the minimum value for the value in the textbox.

Say I want the maximum value to be 237 and the minimum value to be 36. If the value is above that then it is reset to the maximum value. Or if it is below the minimum then it is reset to the minimum value. How would I do this. Also, I am trying not to use a numericupdown controls, so don't bother posting that suggestion.

Thank you in advance for any help that is provided.



Answer this question

trouble setting a max and min value

  • blucas2006

    Hi Troy,

    You can use Regx to validate the input.



  • adman666

    This is what I use:

    Private Sub checkValue(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim tb As TextBox = CType(sender, TextBox)
    Dim min As Byte = 0
    Dim max As Byte = 255
    If sender.Equals(tb2) OrElse sender.Equals(tb4) Then
    min = 1
    max = 16
    End If
    If tb.Text = String.Empty Then Exit Sub
    If CDbl(tb.Text) < min Then tb.Text = CStr(min)
    If CDbl(tb.Text) > max Then tb.Text = CStr(max)
    End Sub

    It works perfect. Also, if the value is blank then it exits instead of throwing an exception. It also handles cases where the min and max values would be different.



  • Simone Poste

    Hi Joe Smugeresky ,

    It is very wrong to hard code the values.



  • Kuba B

    Please give an example of how you would code it.


  • Gimpy

    Not sure how your workflow is but you could use the Leave event:

    Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

    Dim value As Integer = Convert.ToInt32(TextBox1.Text)

    If value < 36 Then
    TextBox1.Text = "36"
    ElseIf value > 237 Then
    TextBox1.Text = "237"
    End If

    End Sub

    -Joe



  • Sambuddha

    Hi,

    try this link and tell me if that helps.

    http://www.c-sharpcorner.com/Language/RegExpressionSample1.asp

    Regards,



  • shanytopper

    Yes, i could be good for you, however this code is hardcoded with all the ifs and consts. You might think to refactor it and use Regx.

  • CRickert

    It was a sample, hardly production code. Samples are meant to be easy to understand, hence the hard coded values.

  • trouble setting a max and min value