Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert data on dynamic textbox windows form C#

Status
Not open for further replies.

hoangluc19

Programmer
Nov 18, 2019
10
0
0
VN
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);
}

}
 
You need to declare the textboxes and button as variables that are global to the form. By declaring them in the method above, they become local to that method and cannot be accessed from outside of that method.

-Dell

Associate Director, Data & Analytics
Protiviti
 
I am a person who does not understand as deeply as you say because my knowledge is not much. So I hope you can help me write the code, thank you very much
 
1. You shouldn't actually have to create all of this yourself. In Visual Studio, if you use the form design functionality it will set it all up correctly for you and you won't have this problem.

2. If you really do need to add objects to a form in you code, you should look at the code that VS produces when using the form designer to get a feel for how it works. Instead of declaring the objects that appear on your form in the method where you're creating them like this:

TextBox textboxUsername = new TextBox();

You will declare the object variables at the top of the class definition for your form like this:

Private TextBox textboxUsername;

This will make the object available throughout your form code.

Then, in your method, you'll initialize them like this:

textboxUsername = new TextBox();

If this is still confusing, you probably need to take a C# class to get the basics down.

-Dell

Associate Director, Data & Analytics
Protiviti
 
Sincerely thank you, you are probably a very considerate and caring family person
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top