Custom NumericUpDown Control

Z

z.sessions

I need a control that acts like a numeric up down control in that
there are two buttons on the right of the control, one for up and one
for down.

The kicker is that the values need to be MM/YY. And if the current
value is 12/08 and the user clicks on up, it needs to change to 01/09.
And if the value is 01/09 and they click on down, it changes to 12/08.
And obviously if it is 03/09 and the click on up it goes to 04/09,
down to 02/09.

The Value property of the NUD control is a Decimal and I see no way to
specify custom formatted values.

So I guess I am going to either need to write a custom NUD control
(hard) or create a usercontrol that is derived from the NUD control
and override the necessary properties and methods to achieve my goal
(sounds easier).

Any suggestions?
 
D

Dom

I need a control that acts like a numeric up down control in that
there are two buttons on the right of the control, one for up and one
for down.

The kicker is that the values need to be MM/YY. And if the current
value is 12/08 and the user clicks on up, it needs to change to 01/09.
And if the value is 01/09 and they click on down, it changes to 12/08.
And obviously if it is 03/09 and the click on up it goes to 04/09,
down to 02/09.

The Value property of the NUD control is a Decimal and I see no way to
specify custom formatted values.

So I guess I am going to either need to write a custom NUD control
(hard) or create a usercontrol that is derived from the NUD control
and override the necessary properties and methods to achieve my goal
(sounds easier).

Any suggestions?

Easily answered. Use the DateTimePicker Control, with the property
"ShowUpDown" set to true. It does everything you want.
 
D

Dom

Easily answered.  Use the DateTimePicker Control, with the property
"ShowUpDown" set to true.  It does everything you want.- Hide quoted text -

- Show quoted text -

I should mention also that you can change the format of the date that
appears in the control. I use "yyyy-MM (MMM)". (Note the upper/lower
case of the format. It's' important). This gives you "2009-01
(Jan)". The name of the month is helpful to users.
 
Z

z.sessions

Easily answered.  Use the DateTimePicker Control, with the property
"ShowUpDown" set to true.  It does everything you want.

This is very close. But the up/down feature only updates one part of
the date value being displayed. I have set the CustomFormat property
to MM/yy. But if the current value is 12/09 and click the up button,
it goes to 01/09. I can switch focus to the year and click it up and
down, but I would like for the value to automatically rollover the
year when advancing or decreasing the month. Is that possible?
 
K

Ken Foskey

This is very close. But the up/down feature only updates one part of the
date value being displayed. I have set the CustomFormat property to
MM/yy. But if the current value is 12/09 and click the up button, it
goes to 01/09. I can switch focus to the year and click it up and down,
but I would like for the value to automatically rollover the year when
advancing or decreasing the month. Is that possible?

Your could create a new class derived from datetime and then handle the
changed event, comparing the old and new values and incrementing the year
automatically when you need to.

Ken
 
Z

z.sessions

Your could create a new class derived from datetime and then handle the
changed event, comparing the old and new values and incrementing the year
automatically when you need to.

I solved this problem with just a little code. I created two class
variables:

Private DateTime currentDateTime;
Private bool changingValue;

Initialize currentDateTime to the Value of the datetimepicker control
and chan gingValue to false in the Constructor.

Then in the datetimepicker control's ValueChanged Event:

if (this.valueChanging)
{
return;
}
this.valueChanging = true;
if (this.dateTimePicker1.Value.Month == 12)
{
if (this.currentDateTimeValue.Month == 11)
{
this.currentDateTimeValue =
this.dateTimePicker1.Value;
}
else
{
this.currentDateTimeValue =
this.dateTimePicker1.Value.AddYears(-1);
this.dateTimePicker1.Value =
this.currentDateTimeValue;
}
}
else if (this.dateTimePicker1.Value.Month == 1)
{
if (this.currentDateTimeValue.Month == 2)
{
this.currentDateTimeValue =
this.dateTimePicker1.Value;
}
else
{
this.currentDateTimeValue =
this.dateTimePicker1.Value.AddYears(1);
this.dateTimePicker1.Value =
this.currentDateTimeValue;
}
}
else
{
this.currentDateTimeValue =
this.dateTimePicker1.Value;
}
this.valueChanging = false;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top