numericupdown problem

How to make a numericupdown control to have the range between 10-2000 in step of 10...and 2000-9900 in step of 100
the code bellow worck's, but with one bug....for eg, if i have 2000 value....and i go down...he go on 1900 value...and i want 1990(if i go up again worck's fine)
is posible to make that?
i search an event ho tell'me in witch way i'm click(up or down)...but i don't find...so, please help'me. tank's
(p.s.:the value 1900 is correct...but the value 2010 is not correct....)

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
If NumericUpDown1.Value >= 2000 Then
NumericUpDown1.Increment = 100
End If
If NumericUpDown1.Value < 2000 Then
NumericUpDown1.Increment = 10
End If
End Sub

[844 byte] By [gtimofte] at [2007-12-24]
# 1

Hi,

I don't think that ValueChanged is a good event for handling this kind of situation, because when user enters the number by typing, the default behavior is that it gets fired only when the control loses focus. I tried to make a new MyNumericUpDown control by deriving from the original one, and overriden its OnTextChanged method. Here's what I got:

Public Class MyNumericUpDown
Inherits NumericUpDown

Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.OnTextChanged(e)

Dim newIncrement As Integer
Dim currentValue As Decimal = Value

If currentValue < 10 Then
newIncrement = 1
ElseIf currentValue < 2000 Then
newIncrement = 10
Else
newIncrement = 100
End If

Increment = newIncrement
End Sub
End
Class

Put this in a separate file and compile it - the control should appear in your toolbox. Drag it on your form, set its Maximum property to 9900 and you're done. I hope it works for you.

You could also further extend the control by adding the ability to set value ranges with different Increment values...

Andrej

AndrejTozon at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Hi!

you ask> How to make a numericupdown control to have the range between 10-2000 in step of 10...and 2000-9900 in step of 100

The first problem is your range requirements. You use 2000 as both your upper range and your lower range. They should be different. Determine where you want the lower range of the 100 delta to be. Is it ok to jump from 2010 to 1910 ? or do you want something more complex like a shrinking delta as you approach the 2000 line such as 2010 to 1990?

Get your rules straight first. Your initial question was too simplistic. When you have determied your rules, then you can formulate a solution.

Ibrahim

IbrahimY at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
tank's for yor answer.....but is worck like the code that i give...with handle of the value.changed.

When i go on 2000, if i go up...go 2100, but if i go down, go on 1900(an i want 1990)
gtimofte at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
tanks

Andrej Tozon for yor answer, but is worck like the code ho i give ....with the handle of valuechanged. (if i go on 2000 value, when i press up, the new value is 2100, but when i press down, the new value is 1900, an i want 1990)

gtimofte at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
sorry for this 2 answer...but he give'me one error, and i answer again..
gtimofte at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
this is what i wont...sow the range is OK.....i don't fink you understand the

problem...sow i repeat: i have a numericupdown control, with the range between 10-9900. between 10 and 2000, the increment value shoult be 10, and between 2000 and 9900, increment=100. this is what i want to do and i don't now how

gtimofte at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

gtimofte wrote:
...if i go on 2000 value, when i press up, the new value is 2100, but when i press down, the new value is 1900, an i want 1990...

Ok,

try overriding UpButton() and DownButton methods, you can set wanted increment value there:

Public Class MyNumericUpDown
Inherits NumericUpDown

Public Overrides Sub DownButton()
Dim newIncrement As Integer
Dim currentValue As Decimal = Value

If currentValue <= 10 Then
newIncrement = 1
ElseIf currentValue <= 2000 Then
newIncrement = 10
Else
newIncrement = 100
End If
Increment = newIncrement
MyBase.DownButton()
End Sub

Public Overrides Sub UpButton()
Dim newIncrement As Integer
Dim currentValue As Decimal = Value

If currentValue < 10 Then
newIncrement = 1
ElseIf currentValue < 2000 Then
newIncrement = 10
Else
newIncrement = 100
End If
Increment = newIncrement
MyBase.UpButton()
End Sub
End
Class

Note: the two methods are *not* the same.

Andrej

AndrejTozon at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

gtimofte wrote:
...if i go on 2000 value, when i press up, the new value is 2100, but when i press down, the new value is 1900, an i want 1990...

Ok,

try overriding UpButton() and DownButton methods, you can set wanted increment value there:

Public Class MyNumericUpDown
Inherits NumericUpDown

Public Overrides Sub DownButton()
Dim newIncrement As Integer
Dim currentValue As Decimal = Value

If currentValue <= 10 Then
newIncrement = 1
ElseIf currentValue <= 2000 Then
newIncrement = 10
Else
newIncrement = 100
End If
Increment = newIncrement
MyBase.DownButton()
End Sub

Public Overrides Sub UpButton()
Dim newIncrement As Integer
Dim currentValue As Decimal = Value

If currentValue < 10 Then
newIncrement = 1
ElseIf currentValue < 2000 Then
newIncrement = 10
Else
newIncrement = 100
End If
Increment = newIncrement
MyBase.UpButton()
End Sub
End
Class

Note: the two methods are *not* the same [value comparison, and you have to set the the corresponding base method]. I hope this works for you and you'll adapt it to your needs.

Andrej

AndrejTozon at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9
tank's for yor answer....worck perfect....tank's one more time
gtimofte at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...