簡體   English   中英

單擊按鈕時更改動態創建的文本框的值C#

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM