C#动态更改值

本文关键字:动态 | 更新日期: 2023-09-27 18:16:51

我是C#的新手,我必须制作一个非常原始的神经元应用程序。它工作得很好,除了一些值不会动态更新。

问题是,当我更改numericUpDown中的一些值时,我希望当前函数(在comboBox中选择(重新计算并更改richTextBox(结果框(中的值

我确信这只是一件大事,但我在任何地方都找不到。

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;
    }
}

如果你需要,我可以提供更多的代码。提前谢谢!

C#动态更改值

NumericUpDown还需要一个事件来重新计算值。

尝试添加这些事件:

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

请注意,Object senderEventArgs e的方法会有所不同,具体取决于调用该方法的事件。

@FatTony

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;
                currentNumUpDown.ValueChanged += new EventHandler(comboBox1_SelectedIndexChanged);
                currentNumUpDown2.ValueChanged += new EventHandler(comboBox1_SelectedIndexChanged);
                MessageBox.Show("executed");
                temp = currentNumUpDown.Value * currentNumUpDown2.Value;
                s = s + temp;
            }
void ValueChanged(object sender, EventArgs e)
    {
        string temp_id = ((NumericUpDown)sender).Name.Remove(0, 14);
        if(temp_id[0]=='0')
        {
            temp_id = temp_id.Remove(0, 1);
        }
        int id = Int32.Parse(temp_id);
        decimal v1, v2;
        var numericUpDown1 = this.panel1.Controls["numericUpDown" + (100+id).ToString()] as NumericUpDown;
        var numericUpDown2 = this.panel1.Controls["numericUpDown" + (200+id).ToString()] as NumericUpDown;
        var CurrentTextBox = this.panel1.Controls["Text" + (100+id).ToString()] as TextBox;
        if (numericUpDown1 != null && numericUpDown2 != null)
        {
            v1 = numericUpDown1.Value;
            v2 = numericUpDown2.Value;
            CurrentTextBox.Text = (v1 * v2).ToString();
        };

这是创建numericUpDown的自定义事件,该事件的值来自另一个numeriUpDown。