簡體   English   中英

在C#中獲取值動態文本框

[英]Get Value Dynamic Textbox in C#

我制作了動態文本框並保留在列表中

private void ConvertButton_Click(object sender, EventArgs e)
{
  List<TextBox> textBoxes = new List<TextBox>();
  foreach (Control item in this.Controls)
  {
    if (item is TextBox)
     {
       TextBox txt = item as TextBox;
       textBoxes.Add(txt);                  
     }

  }        
}

我得到了所有文本框的值,但是我有一個問題。 舉些例子; 如果用戶添加3個標簽,如(A,B,C)並為其添加文本框,如(labelA具有2,labelB具有3,labelC具有1),並且文本框獲得的值類似於數組(textboxes [0]具有值)。問題是我不知道哪個標簽具有哪個值。

我像這樣添加了文本框:

private void addNewTextbox(object sender, EventArgs e)
{
   Button button = (Button)sender;
   List<TextBox> textBoxes = button.Tag as List<TextBox>;
   if (textBoxes == null)
      button.Tag = textBoxes = new List<TextBox>();
   TextBox textBox = new TextBox();
   textBoxes.Add(textBox);
   textBox.Location = new Point(90 * textBoxes.Count, button.Top);
   textBox.Size = new Size(50, 50);
   this.Controls.Add(textBox);
}

例如,我嘗試顯示屏幕;

LabelA-->Textbox1 , Textbox2

</br>

labelB -->Textbox3

</br>

LabelC --> Textbox4 , Textbox5 , TextBox6

每個添加的控件都有一個Name屬性。 使用此屬性可以將不同的控件鏈接在一起。 例如(您應該提出自己的命名約定),您可以這樣做:

LabelA-> TextboxA1,TextboxA2

LabelB->文本框B1

LabelC-> TextboxC1,TextboxC2,TextboxC3

如果您希望有一個更完整的(即復雜的)解決方案,則可以:

  1. 創建您自己的繼承自TextBox
  2. 為關聯的Label控件的名稱添加一個屬性
  3. 實例化新控件時設置此屬性

我正在努力理解您的問題,我想您正在嘗試將用戶創建的文本框鏈接到特定標簽? 如果我完全錯了,請原諒我。 如何將您的代碼構造為如下所示:

注意:使用OP的新代碼進行編輯

    Dictionary<Int, List<TextBox>> label_Textboxes_Dict = new Dictionary<Int, List<TextBox>>();
    List<Label> labelArray = new List<Label>(); //I suggest use list as you don't know the array size beforehand

    void addLabel(int labelNumber)
    {
        int currentLabelArrayCount = labelArray.count; //So that users can add multiple times
        for (int i = currentLabelArrayCount; i < currentLabelArrayCount +labelNumber; i++)
        {
            labelArray.Add(new Label());
            labelArray[i].Text = states[i] + "-->";
            this.Controls.Add(labelArray[i]);
            labelArray[i].Top = 100 + i * 30;
            labelArray[i].Left = 10;
            labelArray[i].Width = 30;
            label_Textboxes_Dict.Add(i, new List<TextBox>());
        }
    }

    void addTextBoxForLabel(int labelNum)
    {
        TextBox t1 = new TextBox();
        TextBox t2 = new TextBox();
        //etc... 

        if (label_Textboxes_Dict.ContainsKey(labelNum))
        {
            label_Textboxes_Dict[labelNum].Add(t1);
            label_Textboxes_Dict[labelNum].Add(t2);
        }
    }

    void doSomethingForAllTextboxesOfLabel(int labelNum)
    {
        List<TextBox> listOfTextBoxes;

        if(label_Textboxes_Dict.TryGetValue(labelNum, out listOfTextBoxes))
        {
            foreach(TextBox tb in listOfTextBoxes)
            {
                //do your stuff
            }
        }
    }
        Label[] labelArray = new Label[10];



        for (int i = 0; i < labelNumber; i++)
        {

            labelArray[i] = new Label();

            labelArray[i].Text = states[i] + "-->";

            this.Controls.Add(labelArray[i]);

            labelArray[i].Top = 100 + i * 30;

            labelArray[i].Left = 10;

            labelArray[i].Width = 30;

        }

我確實像這樣創建了標簽.labelnumber是用戶想要的標簽數量。@ interceptwind

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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