如何动态分配标签页并添加单个文本框,以使用c#将文本框的数据存储到数据库中

问题描述 投票:0回答:1

所以我正在使用Visual Studio .NET创建表单。

当我尝试在单击按钮时动态添加选项卡页时会卡住,这会在新选项卡中创建一个文本框。

当我创建多个选项卡时,我想从每个新创建的选项卡的文本框中获取数据并将其添加到数据库中。我遇到了问题,因为这些文本框具有与动态创建它们相同的名称。我是C#的新手,需要帮助。

 public void add()
        {
            aa.Add(txt.Text);

            var a = 1;
            var newTabPage = new TabPage()

            {
                Text = "Page" 
            };

            txt = new System.Windows.Forms.TextBox();
            this.tabControl1.TabPages.Add(newTabPage);
            newTabPage.Controls.Add(this.txt);

            System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
            newTabPage.Controls.Add(lbl);
            lbl.Text = "New";
            txt.Name = "Get";



            lbl.AutoSize = true;
            lbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lbl.Location = new System.Drawing.Point(7, 389);
            lbl.Name = "label263";
            lbl.Size = new System.Drawing.Size(871, 13);
            lbl.TabIndex = 317;
            lbl.Text = "AA";

newTabPage.Controls.Add(lbl);
        }
        private void txt_TextChanged(object sender, EventArgs e)


        private void button1_Click_1(object sender, EventArgs e)
        {


            add();

            string value = txt.Text;


            aa.Add(value);
           }

private void saveToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            SqlCommand command; 


           string insert1 = @"insert into testing(test) values(@testingt)";

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                try
                {
                    conn.Open();


                    for(int i= 0;i<aa.Count;i++)
                    {

                        if (aa[i]!= ""  && aa[i]!="New Box")
                        {
                            command = new SqlCommand(insert1, conn);

                            command.Parameters.AddWithValue(@"testingt", aa[i]);
                            command.ExecuteNonQuery();
                        }
}
catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
}
c# .net visual-studio windows-forms-designer
1个回答
0
投票

也许您可以通过创建一个增量变量来设置Name属性以在创建它们时加以区别。

int pagecount = 1;
private void btAddtab_Click(object sender, EventArgs e)
{
    TabPage tabPage = new TabPage
    {
        Name = "Tabpage" + pagecount.ToString(),
        Text = "Tabpage" + pagecount.ToString()
    };
    TextBox textBox = new TextBox
    {
        Name = "TextBox" + pagecount.ToString()
    };
    tabPage.Controls.Add(textBox);
    tabControl1.TabPages.Add(tabPage);
    pagecount++;
}

关于如何获取指定的TextBox的值,您可以参考以下代码。

private void btGetValue_Click(object sender, EventArgs e)
{
    foreach (TabPage page in tabControl1.TabPages)
    {
        foreach (Control control in page.Controls)
        {
            // get the first textbox's value
            if (control is TextBox && control.Name == "TextBox1")
            {
                Console.WriteLine(((TextBox)control).Text);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.