dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] [VB.NET-VS2010] Limit the number of digits in a NumericUpDown

  1. #1
    Join Date
    Jul 2012
    Posts
    46

    [RESOLVED] [VB.NET-VS2010] Limit the number of digits in a NumericUpDown

    I have a numericupdown with a minimum set at 0 and a maximum at 100. However, at run-time a user may input an infinite number by skipping the arrows and inputting numbers with keys. I have tried to solve this myself but my error message won't popup until the user presses the enter key.
    Code:
        Private Sub spnMiscQty_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles spnMiscQty.ValueChanged
            If spnMiscQty.Value = 100 Then
                MessageBox.Show("Pick a Quantity between 1 and 100")
            Else
            End If
        End Sub
    What I would really like to do to by-pass this issue is to limit the number (cannot enter a value greater than '100') or to disallow a cursor to be placed in the NumericUpDown (user would only be allowed to use the arrows to select a number).

    Thanks in advance for any help you may provide.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown

    Why? Let the user choose, then change the query to:

    If spnMiscQty.Value > 100 or spnMiscQty.Value < 0 Then ...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2012
    Posts
    46

    Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown

    I posted the wrong code. That code was to check if the message box was working before I found out about the Enter key initiating the msgbox. I did have spnMiscQty.Value > 100 before that.

    I do not want the user to be able to choose a quantity above 100 because this program needs to be airtight. No room for error with my bosses...even user error.

    Thanks for your quick reply David.

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown

    Instead of using valuechanged event, use the Keypress event and trap them there....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,268

    Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown

    Have a look at this thread, it explains why this happens :

    http://www.vbdotnetforums.com/window...html#post20621

    It is the default behaviour. If you want to change that, you will need to make a custom NumericUpDown ( your own ). Not sure if it will be a worthwhile effort
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  6. #6
    Join Date
    Jul 2012
    Posts
    46

    Re: [VB.NET-VS2010] Limit the number of digits in a NumericUpDown

    That's very interesting. I guess I never really took the time to play around with NumericUpDowns before this week. I guess I will leave it as is.

    Thanks for the article Hannes!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)