?
Solved

NumericUpDown control

Posted on 2004-09-20
3
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
KGreg earned 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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