简体   繁体   English

单击按钮时更改动态创建的文本框的值C#

[英]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.

相关问题 单击按钮时更改按钮的文本c# - Change text of button when button is clicked c# 多次单击按钮时,C#更改标签文本 - C# change label text when button clicked multiple times 在 C# WPF 中更改数据网格单元格时动态更改文本框的值 - Dynamically Change value of a text box when changing datagrid cells in C# WPF 如何在动态创建的按钮上更改动态创建的 label 文本单击 C# Windows Z6450242531912981C368Z 应用程序3CAE86 - How to change dynamically created label text on dynamically created button click in C# Windows Forms application 在C#中获取动态创建按钮的文本? - Get the Text of dynamically created button in C#? 选中复选框时动态创建的访问文本框值 - Access text box value created dynamically when check box is checked 单击C#按钮时TinyMCE中没有值 - No value in TinyMCE when C# button is clicked 我在datagridview中添加了更新按钮,当在C#中单击按钮时,如何更改文本? - i have added update button inside datagridview how to change text on it when button is clicked in c#? 如何在单击按钮时从c#中的面板获取动态创建的文本框值 - how to get dynamic created text box value from panel in c# on button click 单击动态创建的按钮时,c#更改动态创建的文本框文本 - c# change dynamic created textbox text when click dynamic created button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM