Chào ae mình có tạo các textbox,button dynamic như này: hàm save không gọi đc các textbox để insert data xuống database
private void Createtextbox()
{
TextBox textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size(500, 30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
TextBox textboxPassword = new TextBox();
textboxPassword.Location = new Point(420, 80);
textboxPassword.Size = new Size(500, 30);
textboxPassword.Name = "text_pass";
this.Controls.Add(textboxPassword);
TextBox textboxMail = new TextBox();
textboxMail.Location = new Point(420, 110);
textboxMail.Size = new Size(500, 30);
textboxMail.Name = "text_mail";
this.Controls.Add(textboxMail);
Button btnSave = new Button();
btnSave.Location = new Point(420, 150);
btnSave.Name = "Submit";
btnSave.Size = new Size(80, 26);
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
}
private void btnSave_Click(object sender, EventArgs e)
{
TextBox textboxUsername = (TextBox)sender;// textboxUsername not instal
}
Hướng dẫn như này mà mình ko hiểu các bạn chuyên sâu gọi các textbox giúp mình với, mình là tay ngang code chả hiểu dc nhiều
//Look closely at your line this.Controls.Add(textboxUsername);You added a control to the form's Controls collection. In your save code, you could enumerate the controls in the same form's Controls collection to find the textbox that you added. How do you know if you found the right control? Well, look at line textboxUsername.Name = "text_user"; where you gave that text box a name. So look for a control with that name and you have found your textbox.