Results 1 to 3 of 3

Thread: NumericUpDown validation

  1. #1

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    NumericUpDown validation

    Hi,

    I'm using a NumericUpDown control on a properties dialog. I've set the Max and Min limits to 1 and 250. The idea is that the user selects a value in that range, clicks the OK button and the value gets passed to the calling code.


    The problem is this: If the user types in 999 (using the keyboard, not the spin buttons) then clicks ok the NumericUpDown control's text and value properties are both 250 and this is what gets passed to the calling code.

    I need to find a way of trapping these invalid entries.

    The two approaches I've already thought of (but don't like are these)
    1. Make the control ReadOnly so that values can't be typed (or pasted) in directly
    2. Monitor the ValueChanged / TextChanged events - I don't like this because if the user wants to change "100" to "110" it throws up an error message for a transitory "1100".


    Any ideas?
    This world is not my home. I'm just passing through.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Monitor the ValueChanged / TextChanged events - I don't like this because if the user wants to change "100" to "110" it throws up an error message for a transitory "1100".
    Then just catch the error, and ignore it instead of showing an error message...and keep the old value instead..

  3. #3

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    RESOLVED

    Thanks to nemaroller:


    VB Code:
    1. Private mblnAddressInvalid As Boolean
    2.  
    3.     Private Sub nudAddress_TextChanged(ByVal sender As Object, _
    4.                                        ByVal e As System.EventArgs) _
    5.                                                 Handles nudAddress.TextChanged
    6.         mblnAddressInvalid = _
    7.                 CBool(Val(CType(sender, NumericUpDown).Text) < 1 OrElse _
    8.                                 Val(CType(sender, NumericUpDown).Text) > 250)
    9.  
    10.     End Sub
    11.  
    12.     Private Sub cmdOK_Click(ByVal sender As System.Object, _
    13.                             ByVal e As System.EventArgs) Handles cmdOK.Click
    14.  
    15.         If mblnAddressInvalid Then
    16.  
    17.             MessageBox.Show(GetText("addressOutOfRange"), _
    18.                             GetText("addressOutOfRangeTitle"), _
    19.                             MessageBoxButtons.OK)
    20.             nudAddress.Focus()
    21.         Else
    22.             DialogResult = DialogResult.OK
    23.         End If
    24.  
    25.     End Sub
    This world is not my home. I'm just passing through.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width