簡體   English   中英

如何使用c#動態分配標簽頁並添加單個文本框以將文本框中的數據存儲到數據庫中

[英]How to assign Tab Pages dynamically and add a single text box to store the data from text box into the database using c#

所以我正在使用 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);
                }
}

也許您可以通過創建增量變量來設置 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);
            }
        }
    }
}

暫無
暫無

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

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