NumericUpDowns

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
I have an issue where if a user enters into a numericupdown and hits "delete" on the keyboard, the value disappears. But the value of the control is still what it was before the user hit "delete".

I like that a user can hit "delete" and wipe out a value on a numericupdown, but how can I get the value to be NULL if this happens? And if I can't get it to be NULL, then how can I get the numericupdown to at least display 0, or the minimum value?

Is there a constant that recognizes that the key pressed, or key down was DELETE? If I can get that established, I can set the value of the control to 0, or minvalue. (Because the value doesn't change, but just disappears when delete is pressed, I can't say "If myNumericUpDown.Value = NULL)
 
VB.NET:
[SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].NumericUpDown1.Text.Length = 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].NumericUpDown1.Value = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].NumericUpDown1.Minimum
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
 
Thanks, this appears to work.

Here's are two more questions, though:

When I deleted the n-updown, the value did indeed go to it's minimum.
But when I deleted the n-updown, at the minimum, the value disappeared again.

In other words: value = 130, I hit delete, now value = 0, I hit delete again, and lose focus, and value =

It's still the minimum value, I'm sure, but the 0 disappears. Not a HUGE problem, but the users can get confused sometimes by stuff like this.

Issue #2...I have a temperature in a n-updown, that I want the minimum to be 90.0, but if a user deletes, I want the value to be null. Or 0, if it must be. At least then, I can code the report to show nothing if value = 0.
In other words, sometimes, they might now want a temperature to be entered. But I still want to enforce a minimum of 90.0 if they DO decide to enter one.

How do I do this in a n-updown? Or can I?
 
since you cannot get the "DELETE" keypress event, then the easy way you can do is maybe add another button beside the n-updown. So when user press then button you can do anything you want.
 
Numeric up down delete key pressed:

VB.NET:
    Private Sub NumericUpDown1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
        If e.KeyCode = Keys.Delete Then
            MsgBox("delete key pressed")
        End If
    End Sub
 
Thanks for the reply, but my main desire was to get the value of the numericupdown to either be null or minimum value (or even 0) when a user hits delete.

I can get it to do it the first time, but if a user hits delete again on a value of 0, the value disappears, but it's STILL the value of the updown, not null.
 
Then the code that DavidT_macktool provide can help you!

VB.NET:
Private Sub NumericUpDown1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
        If e.KeyCode = Keys.Delete Then
            Me.NumericUpDown1.Value = 0
            'or maybe Me.NumericUpDown1.Text = 1
        End If
    End Sub
 
What if you used a variable to hold the selected value. You can manipulate the value anyway you want from many different actions. Without manipulating the actual Up/Down control.

public SelectedNumber as Integer

then:
VB.NET:
    Private Sub NumericUpDown1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
        If e.KeyCode = Keys.Delete Then
            SelectedNumber = 0[COLOR=SeaGreen] ' or [/COLOR][SIZE=2][COLOR=SeaGreen]Me[/COLOR][/SIZE][SIZE=2][COLOR=SeaGreen].NumericUpDown1.Minimum[/COLOR]
[/SIZE]         End If
    End Sub
and
VB.NET:
    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        SelectedNumber = NumericUpDown1.Value
    End Sub
 
Thanks, I figured it out...it was the text problem. I used this code.

If objControl.Text.Length = 0 Then
objControl.Value = objControl.Minimum
objControl.Text = objControl.Minimum.ToString

I could have probably substituted the first line with the If e.keydown, etc., but it works either way.

NOW, if I could just get the value to be NULL, because when the user hits delete, it means they don't want a value. For most that default to 0, this is ok...I can just have the report show no data. But for my temperature field,where the minimum is 90.0, if a user hits delete, the field will default to 90.0. Even if I select 0, or "" as text, the VALUE will still be 90.0. How can I get the VALUE in an updown to be NULL, but still have minimum value enforced if user decides to enter one. Do I need to code this one <sigh>
 
The problem is...when the value is already at its minimum and the user hits delete, the value doesn't change. But see my post above, I fixed the text portion to show the minimum value regardless of whether delete was hit.

But I'd still rather see the value be null or 0 (which is the case for MOST of the updowns, because their minimum value IS 0, but for some that have min values of 90.0 for example (temperature), the value goes to this.

It sounds like I may have to just put the min value to 0 for this control, and enforce a minimum only if the user tries to enter data less than the minimum. I was hoping to avoid that since the upddown is ideal for already doing that.
 
Is that what you want? i confuse with ur question! Not really understand what you looking

VB.NET:
[LEFT]Private Sub NumericUpDown1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
       'If keydown no DELETE that mean user press other button
       If e.KeyCode <> Keys.Delete Then
           'This is what you provide
           objControl.Value = 90
           objControl.Text = "90"
       End If
   End Sub[/LEFT]
 
Thanks, but no.

If the user hits a normal number entry, I want that to go into the value, UNLESS it's less than the minimum value. If they hit delete, I'd prefer the value to be 0 or no, but if the min value is set to 90.0, 0 is not allowed. And I don't think null is allowed regardless for these controls.

Yes, I can code the enforcement of >= 90.0 and set the min value to 0, so if the user hits delete, the value will be 0, but the updown already enforces the 90.0 without code. I may have to compromise though.
 
Back
Top