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...");
}
}