I have to make a quantile configuration and I have to set a condition so that the second value is higher than the first, the third value is higher than the second and so on. I am using numericupdowns and the value is set by the user. I tried to implement this code but it always shows a message box error even if the values are correct. This is my code so far (using Visual studio and C#):
decimal min = 1;
//when the first numeric is changed:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = min;
min = numericUpDown1.Value;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
min ;
numericUpDown2.Minimum = min;
min = numericUpDown2.Value;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
min ;
numericUpDown3.Minimum = min;
min = numericUpDown3.Value;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
min ;
numericUpDown4.Minimum = min;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value )
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
CodePudding user response:
You need to keep all the NumericUpDown value inside a List or an array where you can manipulate them like a single entity trhough a loop.
Of course changing the minimum value of one of the numeric elements should be linked to a check for the current value inside that control because you can't have a current value lesser than the new minimum value.
So the first thing to do is to create a global variable inside your form class where you keep the references to all the numeric controls that you want to synchronize
public class Form1
{
private List<NumericUpDown> numbers = new List<NumericUpDown>();
public Form1 : Form
{
InitializeComponent();
numbers.AddRange(new [] {n1, n2,n3,n4,n5});
}
......
}
Now you can write a method like this one that adjust the minimum on all the numeric included in the list
private void UpdateMinimum()
{
for (int x = 0; x < numbers.Count-1; x )
{
if(numbers[x].Value > numbers[x 1].Value)
numbers[x 1].Value = numbers[x].Value;
numbers[x 1].Minimum = numbers[x].Value;
}
}
finally you have all your NumericUpDown event ValueChanged call the same method
void numerics_ValueChanged(object sender, EventArgs e)
{
UpdateMinimum();
}
CodePudding user response:
If you want to set a condition so that the second value is higher than the first, the third value is higher than the second and so on, you can refer to the following code:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = 1;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
numericUpDown2.Minimum = numericUpDown1.Value 1;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
numericUpDown3.Minimum = numericUpDown2.Value 1;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
numericUpDown4.Minimum = numericUpDown3.Value 1;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value)
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}