0

I currently have a NumericUpDown where users input the number of years they have been at a site.

I then have a DateTimePicker for a DOB field: I want to make sure that the Value specified in the NumericUpDown is not larger than the difference in years between the Date specified in DateTimePicker and the current Date.

For example, the user inputs 10th of january 2010 in the DateTimePicker: they cannot then put a number greater than 10 in the NumericUpDown (or, they can, but it will display an error message).

How would I go about doing this?

2
  • When you say cannot then put a number greater than 10 in the datetimepicker I assume you meant to say numericupdown Commented Mar 12, 2020 at 23:23
  • Yes that is correct sorry I was not entirely awake when I posting this Commented Mar 13, 2020 at 11:17

3 Answers 3

0

Asuming this control https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datetimepicker.value

dateTime SelectedDate = dtp1.Value;
int SelectedLimit = nud1.Value;

if(SelectedDate.Year >= SelectedLimit)
  //do something
else
  //do something else

Of course now the question is how agressiveöy you will prevent wrong settings, where you hook up the change events and the like.

Personally I prefer a rather "passive" Error Reporting via INotifyDataErrorInfo and similar interfaces. Display a message, do not try to force specific values.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, now there's a point on which I occasionally beg to differ - too often "display a message" in winforms means MessageBox.Show... which should be avoided wherever possible in favour of something less obtrusive. In this case if there was a wondering about "why can't I set the NUd to more than 10" it might be appropriate to do away with it entirely - it's redundant to ask when a user moved in and also how many years have they been there and simply creates a potential for confusion and poor UI that will have to explain itself in the case of bad input that is has unnecessarily created
ah terribly sorry in this instance most errors are handled by an invisible label that then has the error message (for lack of knowing the correct term) put into it errorMessage.text = "This is a required field" for example with an extra part that blanks out the label again - probably not the best way it could be handled the actual fields are a DOB field and a years in country field so its to make sure someone does not say they are 20 and then put 30 years
@Txeptsyip Actually that is a perfect use-example for INotifyDataErrorInfo and this validation approach. You have a class that accepts the strings. Does some validation on those strings (inlcuding null check, trying to parse numbers). Block proceeding if it has any errors. And you would expose error messages via said Label(s).
0

How about hooking the date time picker valuechanged event and setting the maximum of the NUD?

private void DateTimePicker1_ValueChanged(Object sender, EventArgs e) {

   numericUpDown1.Maximum = (DateTime.Now.Year - dateTimePicker1.Value.Year);

}

The title of your question and the body text disagree. One says you want to make sure the nud input is larger and the other says not larger. My logic ensures the input in the NUD is not larger, but if you want the opposite, set the NUD Minimum instead of the maximum

DTP has MaxDate and MinDate properties which may also be useful to you in preventing user selections that are too crazy

10 Comments

What if the selected date is for example April 3, 1999? What do you think dateTimePicker1.Value.Year - 2000 will return?
Don't forget that at the time I answered the question it said something very different to what it does now. It has been heavily (and I would say inappropriately) edited by @Jimi - he's put a lot of words in the author's mouth.
At the time I answered the question it said something very different to what it does now and I missed the part at the start where it was 2020-2010=10, instead deciding for the poorly worded second paragraph that it was 2010-2000=10. It has been heavily (and I would say in some case possibly inappropriately) edited by @Jimi - he's put a lot of words in the author's mouth - but it does more heavily labour the current point. Thanks for calling me back so I could make a fix
Well, the edit is very helpful and its now much better post I believe, and doesn't change the OP's requirements. Just wanted to pass you a hint that you might want to consider to give us a better answer.
Helpful or not, i do think it goes a bit too far; we should have pressed the user to better explain themselves rather than made an assumption and written their question for them.. But short of rolling back (which would be a regression too far too) there isn't much else to do - the OP hasn't reappeared to tend his garden (I do always wonder if asking on SO is the last thing some people do before they switch off their PC and go home)
|
0

Well, handle the DateTimePicker.ValueChanged to set the maximum value of the NumericUpDown control:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    numericUpDown1.Maximum = int.Parse(dateTimePicker1.Value.ToString("yy"));
}

If the NumericUpDown shows 4 digits for the year part then just:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    numericUpDown1.Maximum = dateTimePicker1.Value.Year;
}

I then have a DateTimePicker for a DOB field: I want to make sure that the Value specified in the NumericUpDown is not larger than the difference in years between the Date specified in DateTimePicker and the current Date.


Then get the age and assign it to the Maximum property of the NumericUpDown control:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    var age = DateTime.Now.Year - dateTimePicker1.Value.Year;

    if (dateTimePicker1.Value > DateTime.Now.AddDays(-1))
        age -= 1;

    numericUpDown1.Maximum = age;
    numericUpDown1.Value = age;
}

If you prefer to notify the user, then you can use for example the ErrorProvider component. In this case, handle the ValueChanged event of the NumericUpDown control:

//..
//Don't forget to EP.Dispose(); in the FormClosing event.
private readonly ErrorProvider EP = new ErrorProvider();
//..

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    EP.Clear();

    var age = DateTime.Now.Year - dateTimePicker1.Value.Year;

    if (dateTimePicker1.Value > DateTime.Now.AddDays(-1))
        age -= 1;

    if (age < numericUpDown1.Value)
    {
        numericUpDown1.Value = age;
        EP.SetError(numericUpDown1, "Incorrect age...");                
    }                
}

2 Comments

This isn't quite right; the numbers chosen by the OP as an example have seemingly caused confusion for you as they did me. He seems to be doing a "when did you move and and how many years have you lived there" which, while redundant, needs some maths on the difference between the year chosen and now. It's accidentally 10 years because 2020-2010 rather than being 10 as 2010 in YY format.. also, not sure I'd advocate tostringing and parsing to turn a year to two digits. Mod by 100 would be better
@CaiusJard I edited my post accordingly. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.