我有“数量”,“价格”和“总计”文本框,它们都是动态生成的。
我不确定如何在文本更改事件中计算它们。
我为txtBox3和txtBox4创建了TextChanged事件,分别为数量和价格。不知道如何捕获,相乘并显示在动态生成的总文本框中。
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)
{
}
如何从动态文本框中计算数量*从动态文本框中计算价格,并在其textchanged事件中将其显示给动态生成的Total文本框。
答案 0 :(得分:2)
您必须将动态控件添加到控件集合
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();
}
}
答案 1 :(得分:1)
这是完整版本:
//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);
}
答案 2 :(得分:0)
首先,实现查找控制功能,例如:
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;
}
然后,您将获得“总计”文本框:
TextBox txtTotal = (TextBox) this.GetControlByName(this, "txt_Total" + totalTextbox);
TextBox txtPrice = (TextBox) this.GetControlByName(this, "txt_Price" + priceTextbox);
希望有帮助! 干杯-VinhCC