简体   繁体   中英

calculations in dynamically generated controls in windows form applications

I am making a program to learn how to generate controls dynamically inside loop on Button_Click event, now I am trying to calculate multiplication of two fields and their output in third TextBox

Code:

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{


    unit.Location = new System.Drawing.Point(unitstartposition, unitstartpositionV);
    unit.Size = new System.Drawing.Size(100, 30);
    unit.Text = "";
    unit.Name = "unit" + i.ToString();

    rate.Location = new System.Drawing.Point(ratestartposition, ratestartpositionV);
    unit.Size = new System.Drawing.Size(100, 30);
    rate.Text = "";
    rate.Name = "rate" + i.ToString();

    amt.Location = new System.Drawing.Point(amtstartposition, amtstartpositionV);
    amt.Size = new System.Drawing.Size(100, 30);
    amt.Text = "";
    amt.Name = "amt" + i.ToString();

    panel1.Controls.Add(unit);
    panel1.Controls.Add(rate);
    panel1.Controls.Add(amt);


    unitstartpositionV += 30;
    ratestartpositionV += 30;
    amtstartpositionV += 30;

    unit = new TextBox();
    rate = new TextBox();
    amt = new TextBox();


}

Now I want to calculate it like

amt0.Text = (ConvertToInt32(unit0.Text) * ConvertToInt32(rate0.Text)).ToString();
amt1.Text = (ConvertToInt32(unit1.Text) * ConvertToInt32(rate1.Text)).ToString();

and so on

at last I want to sum up all the amt(i). Please help in achieving this.

Starting from the line 'unitstartpositionV += 30;' onward, here's what I've come up with to solve this:

            unitstartpositionV += 30;
            ratestartpositionV += 30;
            amtstartpositionV += 30;

            unit = new TextBox();
            rate = new TextBox();
            amt = new TextBox();
            unit.Tag = rate;
            rate.Tag = amt;
            amt.Tag = unit;


            rate.TextChanged += rate_TextChanged;
            unit.TextChanged += unit_TextChanged;
        }
    }

    private void rate_TextChanged(object sender, EventArgs e)
    {

        var rate = sender as TextBox;
        var amt = rate.Tag as TextBox;
        var unit = amt.Tag as TextBox;
        Calc(rate.Text, unit.Text, amt);
    }

    private void unit_TextChanged(object sender, EventArgs e)
    {
        var unit = sender as TextBox;
        var rate = unit.Tag as TextBox;
        var amt = rate.Tag as TextBox;
        Calc(rate.Text, unit.Text, amt);
    }

    private void Calc(string rate, string unit, TextBox amt)
    {
        int rateInt;

        if (!int.TryParse(rate, out rateInt)) return;

        int unitInt;
        if (!int.TryParse(unit, out unitInt)) return;

        amt.Text = (rateInt * unitInt).ToString();
    }

(It doesn't include the sum of all)

I've solved the problem here in an unusual way, to fit in with the style you've shown. But really you should be using arrays, or lists, or some kind of 'collection' to hold your controls. From reviewing your other answers I don't think you've learnt about IEnumerable yet. That's the concept you're missing that would make this kind of activity much simpler (particularly the 'sum of all' requirement).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM