×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Insert data on dynamic textbox windows form C#

Insert data on dynamic textbox windows form C#

Insert data on dynamic textbox windows form C#

(OP)
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);
}

}

RE: Insert data on dynamic textbox windows form C#

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
www.protiviti.com

RE: Insert data on dynamic textbox windows form C#

(OP)
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

RE: Insert data on dynamic textbox windows form C#

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
www.protiviti.com

RE: Insert data on dynamic textbox windows form C#

(OP)
Sincerely thank you, you are probably a very considerate and caring family person

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close