Solved

NumericUpDown control

Posted on 2004-09-20
3
341 Views
Last Modified: 2008-02-26
Quick Question.

Is there a setting or code to prevent someone from typing over the maximum value set for a NumericUpDown control.
If the max is set to 100 you cant scroll pass but you can type whatever ou want...

Thanks in Advance for the Info.
0
Comment
Question by:swilli6926
3 Comments
 
LVL 6

Accepted Solution

by:
KGreg earned 50 total points
ID: 12105757
put this in the keypress event or the numeric editor (mine was named num):

        If Val(num.Text + e.KeyChar.ToString) > num.Maximum Then
            num.Value = num.Maximum
            num.Text = num.Maximum.ToString
            e.Handled = True
        End If

KGreg
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12105770
you can type in number bigger than the max
but when you get value you will find the value is set to the max
0
 

Expert Comment

by:St_Rand
ID: 26005641
KGreg, you are not quite right.  You must consider the placement of the cursor.  For example if the maximum value allowed is 45, follow this example:
1. there is one number entered already. Let's say its 3
2. The user then puts the cursor before the 3 and types a 5.  The resulting value would be 53, which is greater than the maximum value allowed, BUT your code If Val(num.Text + e.KeyChar.ToString) > num.Maximum  would test "35" and allow the keystroke.  You need to do something like this:
    Private Sub num_KeyPress(ByVal sender As Object,   _
         ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles num.KeyPress
         Dim s As String
         s = num.Text
         'place the pressed character into the
          'proper position within the text - then you can test for maximum value.
         s = s.Insert(num.SelectionStart, e.KeyChar.ToString)
              If Val(s + e.KeyChar.ToString) > num.Maximum Then
                  num.Value = num.Maximum
                  num.Text = num.Maximum.ToString
                  e.Handled = True
             End If
    End Sub
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question