C# 如何检索动态创建的文本字段的输入值?

C# 如何检索动态创建的文本字段的输入值?,c#,arrays,winforms,dynamic,textbox,C#,Arrays,Winforms,Dynamic,Textbox,这是到目前为止我的代码,其中有一个名为numericupdown和button的numericupdown项。一旦用户在按下按钮时选择了一个数字,它就会动态地创建字段 private void createPerson_Click(object sender, EventArgs e) { Label[] person_Name = new Label[(int)this.numericUpDown.Value]; TextBox[] person_txtinput =

这是到目前为止我的代码,其中有一个名为numericupdown和button的numericupdown项。一旦用户在按下按钮时选择了一个数字,它就会动态地创建字段

private void createPerson_Click(object sender, EventArgs e)
{    
    Label[] person_Name = new Label[(int)this.numericUpDown.Value];
    TextBox[] person_txtinput = new TextBox[(int)this.numericUpDown.Value];

    for (int i = 0; i < this.numericUpDown.Value; i++)
    {    
        //create person name label 
        Person_Name[i] = new Label();
        Person_Name[i].Location = new System.Drawing.Point(20, 114 + i * 25);
        Person_Name[i].Size = new System.Drawing.Size(120, 15);
        Person_Name[i].Text = (i + 1).ToString() + @")" + "Person Name:";
        this.Controls.Add(Person_Name[i]);

        //create person name textbox
        PersonNameTxtInput[i] = new TextBox();
        PersonNameTxtInput[i].Location = new System.Drawing.Point(140, 114 + i * 25);
        PersonNameTxtInput[i].Size = new System.Drawing.Size(125, 20);
        this.Controls.Add(PersonNameTxtInput[i]);    
    }    
}



 private void save_Click(object sender, EventArgs e)
{
    for (int i = 0; j < this.numericUpDown.Value; i++)
    {
        MessageBox.Show("" + PersonNameTxtInput[i].Text);
    }
}
private void createPerson\u单击(对象发送者,事件参数e)
{    
Label[]person_Name=新标签[(int)this.numericUpDown.Value];
TextBox[]person_txtinput=新的TextBox[(int)this.numericUpDown.Value];
for(int i=0;i
我的问题是,如何根据用户在按下“保存”按钮时创建的字段数量从文本框中获取所有值


我已尝试在保存按钮侦听器中使用代码,但是如何使
Label[]person\u Name=new Label[(int)this.numericUpDown.Value]一个全局变量,这样我就可以在循环的保存按钮中访问它。

好吧,我不知道你为什么要用这种特殊的方式来做,我必须承认它似乎不是很有效,但是你可以按照Ryan_L的建议去做,然后迭代。像这样的控件

for(int i = 0; i < this.Controls.Count; i++)
{
    if(this.Controls[i] is TextBox) //skip buttons and labels
    {
        MessageBox.Show("" + this.Controls[i].Text);
    }
}

希望这有帮助。但是,您可能需要重新考虑您的整个方法。

好吧,我不知道您为什么要以这种特殊的方式这样做,我必须承认这似乎不是很有效,但您可以按照Ryan\u L的建议做,并反复使用这种方法。像这样的控件

for(int i = 0; i < this.Controls.Count; i++)
{
    if(this.Controls[i] is TextBox) //skip buttons and labels
    {
        MessageBox.Show("" + this.Controls[i].Text);
    }
}

希望这有帮助。但是,您可能需要重新考虑您的整个方法。

有什么原因您不能重复使用此控件吗?您能详细说明您的问题吗?我对编码很陌生。如果我遍历这个。控件,我也会得到标签名。我只需要文本字段中的值,而不是标签。这是我写的一些代码。有什么原因你不能重复这个。控件吗?你能详细回答你的问题吗?我对编码很陌生。如果我遍历这个。控件,我也会得到标签名。我只需要文本字段中的值,而不是标签。这是我写的一些代码。嘿,我试过你的方法,它可以工作,但是有没有一种方法可以访问一组特定的文本字段。例如,如果用户从numericupdown字段中选择2,因此创建了两个字段person name、person Lastname,我将如何在变量中存储每个值。这样做的目的是我需要从用户那里获取输入并保存到xml文件中。我不确定我是否正确理解了这个问题,但如果我确实理解了,您可以将每个值存储到字符串列表中。因此,与MessageBox.Show不同,您将有如下内容:
myList.Add(this.Controls[i].Text)然后,在for循环之后,您可以序列化列表。请参阅线程以了解如何序列化列表。我设法使用我的方法来实现这一点,似乎我为要保存的数据指定了错误的位置;s谢谢你的帮助。嘿,我已经尝试过你的方法,它很有效。但是,有没有办法找到一组特定的文本字段。例如,如果用户从numericupdown字段中选择2,因此创建了两个字段person name、person Lastname,我将如何在变量中存储每个值。这样做的目的是我需要从用户那里获取输入并保存到xml文件中。我不确定我是否正确理解了这个问题,但如果我确实理解了,您可以将每个值存储到字符串列表中。因此,与MessageBox.Show不同,您将有如下内容:
myList.Add(this.Controls[i].Text)然后,在for循环之后,您可以序列化列表。请参阅线程以了解如何序列化列表。我设法使用我的方法来实现这一点,似乎我为要保存的数据指定了错误的位置;s谢谢你的帮助