[英]Change Value of a Dynamically Created Text Box when button is clicked C#
我使用自己创建的功能动态创建了一个按钮,该功能旨在增加使用相似功能创建的另一个按钮的值。 我可以检索动态创建的文本框的名称,但是由于它是动态创建的,因此无法引用它。
我尝试将文本框作为参数传递,但似乎无法传递额外的参数:
Button ButtonDecreaseValue= addButton(ItemName, "-");
TextBox TextBoxItemAmount = addTextBox(ItemName, "0");
因此, 当单击ButtonRemoveItem
时 , 如何更改文本框一的值?
我为按钮创建了一个事件处理程序,如下所示:
ButtonIncreaseValue.Click += new EventHandler(ItemDecreased);
但是,在事件处理程序内部,我不确定如何更改文本框名称。
private void ItemDecreased(object sender, EventArgs e)
{
Button currentItem = (Button)sender;
int ItemNo = Convert.ToInt32(currentItem.Tag);
DataRow ItemInfo = getItemDetails(ItemNo);
ItemInfo[0].ToString();
}
单击按钮时,相应的文本框的值应减小。
假设这是Windows窗体
您说您有TextBox
的名称吗? 您应该能够找到它。
private void ItemDecreased(object sender, EventArgs e)
{
string textBoxName = HoweverYouGetTheDynamicTextBoxName();
// Assumptions:
// 1. ItemDecreased is a method on a Form or a UserControl.
// 2. There is only one TextBox with the given name in the Form or UserControl.
// 3. The TextBox is added to the Form or UserControl's Controls property.
TextBox tb = Controls.Find(textBoxName, true).OfType<TextBox>().SingleOrDefault();
if (tb is null)
{
// Couldn't find the text box for some reason.
}
// tb references the dynamically created text box.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.