Boanta Ionut Boanta Ionut - 26 days ago 7
C# Question

C# dynamically change the value

I am pretty new at C# and I had to make a really primitive neuron app.
It works pretty good, except some values that don't update dynamically.

The problem is that when I change some values in the

numericUpDown
I want the current function(selected in a
comboBox
) to recalculate and change the value in the
richTextBox
(the result box)

I am sure it's just an event thing, but I couldn't find it anywhere.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.Text=="Suma")
{
decimal s = 0;
decimal temp;
for (int i = 0; i < numericUpDown1.Value; i++)
{
var currentNumUpDown = this.panel1.Controls["numericUpDown" + (100 + i).ToString()] as NumericUpDown;
var currentNumUpDown2 = this.panel1.Controls["numericUpDown" + (200 + i).ToString()] as NumericUpDown;
temp = currentNumUpDown.Value * currentNumUpDown2.Value;
s = s + temp;
}
richTextBox1.Text = s.ToString();
Fin = s;
}
if (comboBox1.Text == "Produs")
{
decimal p = 1;
decimal temp;
for (int i = 0; i < numericUpDown1.Value; i++)
{
var currentNumUpDown = this.panel1.Controls["numericUpDown" + (100 + i).ToString()] as NumericUpDown;
var currentNumUpDown2 = this.panel1.Controls["numericUpDown" + (200 + i).ToString()] as NumericUpDown;
temp = currentNumUpDown.Value * currentNumUpDown2.Value;
p = p * temp;
}
richTextBox1.Text = p.ToString();
Fin = p;
}
if (comboBox1.Text == "Minim")
{
decimal minim = 2;
decimal temp;
for (int i = 0; i < numericUpDown1.Value; i++)
{
var currentNumUpDown = this.panel1.Controls["numericUpDown" + (100 + i).ToString()] as NumericUpDown;
var currentNumUpDown2 = this.panel1.Controls["numericUpDown" + (200 + i).ToString()] as NumericUpDown;
temp = currentNumUpDown.Value * currentNumUpDown2.Value;
if (minim > temp)
minim = temp;
}
richTextBox1.Text = minim.ToString();
Fin = minim;
}
if (comboBox1.Text == "Maxim")
{
decimal maxim = -2;
decimal temp;
for (int i = 0; i < numericUpDown1.Value; i++)
{
var currentNumUpDown = this.panel1.Controls["numericUpDown" + (100 + i).ToString()] as NumericUpDown;
var currentNumUpDown2 = this.panel1.Controls["numericUpDown" + (200 + i).ToString()] as NumericUpDown;
temp = currentNumUpDown.Value * currentNumUpDown2.Value;
if (maxim < temp)
maxim = temp;
}
richTextBox1.Text = maxim.ToString();
Fin = maxim;
}
}


I can provide more code if you need it. Thank you in advance!

Answer

You also need an event for the NumericUpDown to recalculate the value.

try adding these events:

currentNumUpDown.ValueChanged += new EventHandler(comboBox1_SelectedIndexChanged); 
currentNumUpDown2.ValueChanged += new EventHandler(comboBox1_SelectedIndexChanged);

Note that Object sender and EventArgs e will differ in the method, depending on which event called the method.

Comments