hoangluc19
Programmer
i create dynamic textbox and button save app windows form, but i can't insert data to dynamic textbox after i create event button click save
private void Createtextbox()
{
TextBox textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size(500, 30);
textboxUsername.Name = "text_user";
System.Web.UI.WebControls.RequiredFieldValidator rq = new System.Web.UI.WebControls.RequiredFieldValidator();
rq.ErrorMessage = "Error is for Dynamic Control";
rq.BorderColor = Color.Red;
rq.ControlToValidate = "DynControl";
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)
{
try
{
if (this.ValidateChildren())
{
//Here the form is in valid state
//Do what you need when the form is valid
TextBox textboxUsername = (TextBox)sender;// textboxUsername not instal
}
else
{
var listOfErrors = this.errorProvider1.ContainerControl.Controls.Cast<Control>()
.Select(c => this.errorProvider1.GetError(c))
.Where(s => !string.IsNullOrEmpty(s))
.ToList();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Createtextbox()
{
TextBox textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size(500, 30);
textboxUsername.Name = "text_user";
System.Web.UI.WebControls.RequiredFieldValidator rq = new System.Web.UI.WebControls.RequiredFieldValidator();
rq.ErrorMessage = "Error is for Dynamic Control";
rq.BorderColor = Color.Red;
rq.ControlToValidate = "DynControl";
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)
{
try
{
if (this.ValidateChildren())
{
//Here the form is in valid state
//Do what you need when the form is valid
TextBox textboxUsername = (TextBox)sender;// textboxUsername not instal
}
else
{
var listOfErrors = this.errorProvider1.ContainerControl.Controls.Cast<Control>()
.Select(c => this.errorProvider1.GetError(c))
.Where(s => !string.IsNullOrEmpty(s))
.ToList();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}