0

In C# WinForms desktop application I use 2 interdependent numericUpDown1 min and numericUpDown2 max value numericUpDown controls:

private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
{
    if (numericUpDown1.Value <= numericUpDown2.Value)
    {
        min = (int)numericUpDown1.Value;
    } 
    else
    {
        numericUpDown1.Value = min - 1;
    }
}

private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
    if (numericUpDown2.Value >= numericUpDown1.Value)
    {
        max = (int)numericUpDown2.Value;
    }
    else
    {
        numericUpDown2.Value = max + 1;
    }
} 

with using of ReadOnly = true; to avoid making the maximal number less than minimal manually from numericUpDown input.:

        min = 20;
        max = 1999;

        numericUpDown1.Value = min;
        numericUpDown2.Value = max;

        numericUpDown1.ReadOnly = true;
        numericUpDown2.ReadOnly = true;

        numericUpDown1.Increment = 1;
        numericUpDown2.Increment = 1;

        numericUpDown1.Maximum = 2000;
        numericUpDown1.Minimum = 1;
        numericUpDown2.Maximum = 2000;
        numericUpDown2.Minimum = 1;

but I use a big range from 1 to 2000, and want to allow the user to change the number of numericUpDown manually with ReadOnly = false;.

I'm trying to figure out, how to control the user input condition with ReadOnly = false; of numericUpDown to avoid the input of maximal number less than minimal or minimal bigger then maximal.

1
  • hmmm but it will changed to min / max value on lost focus ... where is the problem ? Just use Minimum and Maximum properties Commented Sep 22, 2021 at 9:38

1 Answer 1

0

Try this solution:

First of all Set your numericUpDown property ReadOnly=false or write a line of code in your FormLoad_Function,

numericUpDown1.ReadOnly = false;
numericUpDown2.ReadOnly = false;

Then

private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
{
    if (numericUpDown1.Value > numericUpDown2.Value)
    {
        numericUpDown1.Value = numericUpDown1.Value - 1;
        MessageBox.Show("Min value always less then Max value");
    } 
}

private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
    if (numericUpDown1.Value > numericUpDown2.Value)
    {
        numericUpDown2.Value = numericUpDown2.Value + 1;
        MessageBox.Show("Max value always greater then Min value");
    } 
} 
Sign up to request clarification or add additional context in comments.

Comments

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.