Solved

NumericUpDown control

Posted on 2004-09-20
3
340 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now