[英]Change Value of a Dynamically Created Text Box when button is clicked C#
I have dynamically created a button using a function I made which is meant to increase the value of another button created using a similar function. 我使用自己创建的功能动态创建了一个按钮,该功能旨在增加使用相似功能创建的另一个按钮的值。 I'm able to retrieve the name of the dynamically created textbox, but since it is dynamically created, I can't reference it.
我可以检索动态创建的文本框的名称,但是由于它是动态创建的,因此无法引用它。
I've tried to pass the textbox through as a parameter, but it doesn't appear that I'm able to pass extra parameters: 我尝试将文本框作为参数传递,但似乎无法传递额外的参数:
Button ButtonDecreaseValue= addButton(ItemName, "-");
TextBox TextBoxItemAmount = addTextBox(ItemName, "0");
So, how would I change the value of textbox one when ButtonRemoveItem
is clicked? 因此, 当单击
ButtonRemoveItem
时 , 如何更改文本框一的值?
I've created an event handler for the button, like so: 我为按钮创建了一个事件处理程序,如下所示:
ButtonIncreaseValue.Click += new EventHandler(ItemDecreased);
But, inside the event handler, I'm unsure how I would change the textbox name. 但是,在事件处理程序内部,我不确定如何更改文本框名称。
private void ItemDecreased(object sender, EventArgs e)
{
Button currentItem = (Button)sender;
int ItemNo = Convert.ToInt32(currentItem.Tag);
DataRow ItemInfo = getItemDetails(ItemNo);
ItemInfo[0].ToString();
}
When clicking the button, the corresponding textbox should decrease in value. 单击按钮时,相应的文本框的值应减小。
Assuming this is Windows Forms 假设这是Windows窗体
You said you have the name of the TextBox
? 您说您有
TextBox
的名称吗? You should be able to find it with that. 您应该能够找到它。
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.