我使用我创建的函数动态创建了一个按钮,该函数旨在增加使用类似函数创建的另一个按钮的值。我能够检索动态创建的文本框的名称,但由于它是动态创建的,因此我无法引用它。
我试图将文本框作为参数传递,但似乎无法传递额外的参数:
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.
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句