简体   繁体   中英

How to calculate values from 2 dynamic textboxes and displaying in third dynamic textbox?

I have Quantity, Price and Total textboxes, all are generating dynamically.

I am not sure how to calculate them on their text changed event.

I created TextChanged events for txtBox3 and txtBox4 which are qty and price respectively. not sure how to capture, multiply them and show in dynamically generated total textbox.

        TextBox txtBox3 = new TextBox();
        txtBox3 = new TextBox();
        txtBox3.Location = new Point(313, position);
        txtBox3.Visible = true;
        txtBox3.Name = "txt_QTY" + qtyTextbox;
        txtBox3.TextChanged += txtBox3_TextChanged;
        txtBox3.KeyPress += txtBox3_KeyPress;
        qtyTextbox++;

        TextBox txtBox4 = new TextBox();
        txtBox4 = new TextBox();
        txtBox4.Location = new Point(447, position);
        txtBox4.Visible = true;
        txtBox4.Name = "txt_Price" + priceTextbox;
        txtBox4.TextChanged += txtBox4_TextChanged;
        txtBox4.KeyPress += txtBox4_KeyPress;
        priceTextbox++;


        TextBox txtBox5 = new TextBox();
        txtBox5 = new TextBox();
        txtBox5.Location = new Point(556, position);
        txtBox5.Visible = true;
        txtBox5.Name = "txt_Total" + totalTextbox;
        totalTextbox++;
        panel1.Controls.Add(txtBox3);
        panel1.Controls.Add(txtBox4);
        panel1.Controls.Add(txtBox5);

    private void txtBox4_TextChanged(object sender, EventArgs e)
    {

    }

    private void txtBox3_TextChanged(object sender, EventArgs e)
    {

    }

How do I calculate QTY from dynamic textbox * Price from dynamic textbox and show it to dynamically generated Total textbox on their textchanged event.

You must add your dynamic control into controls collection

int controlCounts = 0;

private void addControls_Click(object sender, EventArgs e)
{

    controlCounts++;

    TextBox txt_QTY = new TextBox();
    txt_QTY.Location = new Point(100 * controlCounts, 100);
    txt_QTY.Name = "txt_QTY" + controlCounts;
    Controls.Add(txt_QTY);
    txt_QTY.TextChanged += txt_QTY_TextChanged;

    TextBox txt_Price = new TextBox();
    txt_Price.Location = new Point(100 * controlCounts, 200);
    txt_Price.Name = "txt_Price" + controlCounts;
    Controls.Add(txt_Price);
    txt_Price.TextChanged += txt_Price_TextChanged;

    TextBox txt_Total = new TextBox();
    txt_Total.Location = new Point(100 * controlCounts, 300);
    txt_Total.Name = "txt_Total" + controlCounts;
    Controls.Add(txt_Total);
}

private void txt_QTY_TextChanged(object sender, EventArgs e)
{
    TextBox txt_QTY = (TextBox)sender;
    string index = txt_QTY.Name.Substring("txt_QTY".Length);
    updateTotal(index);
}

private void txt_Price_TextChanged(object sender, EventArgs e)
{
    TextBox txt_Price = (TextBox)sender;
    string index = txt_Price.Name.Substring("txt_Price".Length);
    updateTotal(index);
}

private void updateTotal(string index)
{
    TextBox txt_QTY = (TextBox)Controls["txt_QTY" + index];
    TextBox txt_Price = (TextBox)Controls["txt_Price" + index];
    TextBox txt_Total = (TextBox)Controls["txt_Total" + index];

    if ((txt_QTY.Text != "") && (txt_Price.Text != ""))
    {
        txt_Total.Text = (Convert.ToInt32(txt_QTY.Text) * Convert.ToInt32(txt_Price.Text)).ToString();
    }

}

Here is a complete version:

//Add tag
String tag = "";

        TextBox txtBox3 = new TextBox();
        txtBox3 = new TextBox();
        txtBox3.Location = new Point(313, position);
        txtBox3.Visible = true;
        txtBox3.Name = "txt_QTY" + qtyTextbox;
        txtBox3.TextChanged += txtBox3_TextChanged;
        txtBox3.KeyPress += txtBox3_KeyPress;

tag = "txt_QTY" + qtyTextbox;

        qtyTextbox++;

        TextBox txtBox4 = new TextBox();
        txtBox4 = new TextBox();
        txtBox4.Location = new Point(447, position);
        txtBox4.Visible = true;
        txtBox4.Name = "txt_Price" + priceTextbox;
        txtBox4.TextChanged += txtBox4_TextChanged;
        txtBox4.KeyPress += txtBox4_KeyPress;

tag += "," + "txt_Price" + priceTextbox;

        priceTextbox++;


        TextBox txtBox5 = new TextBox();
        txtBox5 = new TextBox();
        txtBox5.Location = new Point(556, position);
        txtBox5.Visible = true;
        txtBox5.Name = "txt_Total" + totalTextbox;
        totalTextbox++;

tag += "," + "txt_Total" + totalTextbox;


//Set same tag into three 3 textbox
      txtBox3.Tag = txtBox4.Tag =  txtBox5.Tag = tag;

Add a parse function:

 private TextBox[] getTextBoxFromTag(String Tag)
    {
    TextBox [] arrTextBox = new [3] TextBox();
    String arrTag[] = Tag.Split(",");
    //Harcode 
    arrTextBox[0] = GetControlByName(this, arrTag[0]);
    arrTextBox[1] = GetControlByName(this, arrTag[1]);
    arrTextBox[2] = GetControlByName(this, arrTag[2]);
    return arrTextBox;
    }

public Control GetControlByName(Control ParentCntl, string NameToSearch)
    {
        if (ParentCntl.Name == NameToSearch)
            return ParentCntl;

        foreach (Control ChildCntl in ParentCntl.Controls)
        {
            Control ResultCntl = GetControlByName(ChildCntl, NameToSearch);
            if (ResultCntl != null)
                return ResultCntl;
        }
        return null;
}

void updateTotal(object sender){
    String tag = ((TextBox)sender).Tag;
    TextBox [] txt = getTextBoxFromTag(tag);
    txt[2].Text = Convert.ToInt32(txt[0].Text) * Convert.ToInt32(txt[1].Text);
}
private void txt_QTY_TextChanged(object sender, EventArgs e)
{
    updateTotal(sender);
}

private void txt_Price_TextChanged(object sender, EventArgs e)
{
    updateTotal(sender);
}

First, implement a find control function, eg:

public Control GetControlByName(Control ParentCntl, string NameToSearch)
{
    if (ParentCntl.Name == NameToSearch)
        return ParentCntl;

    foreach (Control ChildCntl in ParentCntl.Controls)
    {
        Control ResultCntl = GetControlByName(ChildCntl, NameToSearch);
        if (ResultCntl != null)
            return ResultCntl;
    }
    return null;
}

Then, you can get the Total Textbox:

    TextBox txtTotal = (TextBox) this.GetControlByName(this, "txt_Total" + totalTextbox);
    TextBox txtPrice = (TextBox) this.GetControlByName(this, "txt_Price" + priceTextbox);

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