C# 存储动态创建的多个文本框中的数据

C# 存储动态创建的多个文本框中的数据,c#,textbox,C#,Textbox,我使用一个按钮创建了一组文本框,如下所示: public partial class Form1 : Form { private int a = 75; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) {

我使用一个按钮创建了一组文本框,如下所示:

public partial class Form1 : Form
    {
        private int a = 75;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox1.Location = new System.Drawing.Point(50, a);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            this.Controls.Add(this.textBox1);

            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox2.Location = new System.Drawing.Point(200, a);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 0;
            this.Controls.Add(this.textBox2);

            this.button1.Location = new System.Drawing.Point(650, a);
            a += 25;
        }
  }
因此,可能会为许多按钮单击创建许多textbox1和textbox2。说10 textbox1和10 textbox2。如何从所有这些数据库中获取数据并存储在数据库中。每个textbox1和textbox2是否在表格的一行中


欢迎任何帮助

首先:我想您正在寻找一个
DataGrid

如果没有,则需要采取若干步骤。首先,
textbox1
textbox2
是类的成员,而不是方法的成员,因此每次单击都会反复编辑相同的文本框。要创建新的文本框,请使用

TextBox newBox = new TextBox();  
在方法内部,继续使用它而不是
this.textbox1
(或
textbox2
)。请记住,每次单击按钮时都应该更改文本框的位置,以便它们不会重叠。可以使用类变量作为click count

我认为最简单的方法是将创建的文本框存储在集合中,例如字典:

public partial class...
{
    Dictionary<TextBox, TextBox> tbPairs = new Dictionary<TextBox, TextBox>();

    private void button1_Click(...)
    {

    ... //create newBox1 and newBox2

    tbPairs[newBox1] = newBox2;   //adds the Pair to the Dictionary
    }
}
公共部分类。。。
{
字典tbPairs=新字典();
专用无效按钮1\u单击(…)
{
…//创建newBox1和newBox2
tbPairs[newBox1]=newBox2;//将该对添加到字典中
}
}
要在文本框填充后回忆词典的内容,请使用

foreach (KeyValuePair<TextBox, TextBox> in tbPairs)  
{  
    ... //write to Server here - but that is an issue too big for handling here - look it up
}
foreach(tbPairs中的KeyValuePair)
{  
…//在此处写入服务器-但这是一个太大的问题,无法在此处处理-请查找它
}
有关如何将数据写入数据库,请查看如何使用
SqlCommand
s