Question

I have 2 NumericUpDown controls in a winforms application for a Min/Max value. I want to do something so that if the MAX is lets say 30, the MIN value should not increase over 29, and if lets say the MIN value is currently 20, the MAX value should not go lover than 21.

So what I want is that there should be always 1 between MIN and MAX value.

I tried to logic this like the code below but it does not work! What is wrong?

private void numericUpDownChartMin_ValueChanged(object sender, EventArgs e)
{
    var value = numericUpDownChartMin.Value; //Current value

    if (value < numericUpDownChartMax.Value) //if value < MAX
        tempChart.ChartStyle.MinimumValue = value; //Use the value
    else
        numericUpDownChartMin.Value = value; //Keep the value the same
}

private void numericUpDownChartMax_ValueChanged(object sender, EventArgs e)
{
    var value = numericUpDownChartMax.Value; //Current value

    if (value > numericUpDownChartMin.Value) //if value > MAX
        tempChart.ChartStyle.MaximumValue = value; //Use the value
    else
        numericUpDownChartMax.Value = value; //Keep the value the same
}

EXAMPLE!!!!

The upDownMÍN current value is 20 and upDownMax current value is 30. So user can change the upDownMin value up to 29.

if upDownMAX is being increased to say 40, the user can set upDownMIN to 39.

Same thing for upDownMAX.....user should not be able to set the max value lower than the upDownMIN value.

Was it helpful?

Solution

    private void numericUpDownChartMin_ValueChanged(object sender, EventArgs e)
    {
         numericUpDownChartMax.Minimum = numericUpDownChartMin.Value + 1;
    }

    private void numericUpDownChartMax_ValueChanged(object sender, EventArgs e)
    {
         numericUpDownChartMin.Maximum = numericUpDownChartMax.Value - 1;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top