Solved

NumericUpDown control

Posted on 2004-09-20
3
344 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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