Insert data on dynamic textbox?

HoangLuc19

Member
Joined
Nov 18, 2019
Messages
12
Programming Experience
1-3
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
C#:
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);
}

}
 
Last edited by a moderator:
In the future, please post your code in code tags. Use the button that looks like </>.

Moving this to WinForms... I don't think this is a .NET Core issue.
 
The issue is that in normal usage, the sender for a button click event will be the button itself. On line 44, why do you think that it would be the textbox that would be the sender?
 
Notice how you added controls into the forms control collection? You can search for your added textbox from that collection.
 
Notice how you added controls into the forms control collection? You can search for your added textbox from that collection.
the load function I call the Createtextbox function to let the textboxes and buttons display on the form and I enter the data to insert into the database, but I can't call those textboxes
 
Again. Look closely at your line 11. 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 6 where you gave that text box a name. So look for a control with that name and you have found your textbox.

As a quick aside, the web 3.0 style of dynamically adding a control to a page is not the WinForms way doing things. The WinForms way of doing things is to actually have the controls on the page, but have them hidden until it is time to show them -- basically the Web 1.0 way of doing things. If you did the old style, then you would have references to all those controls as members of your form class.
 
Again. Look closely at your line 11. 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 6 where you gave that text box a name. So look for a control with that name and you have found your textbox.

As a quick aside, the web 3.0 style of dynamically adding a control to a page is not the WinForms way doing things. The WinForms way of doing things is to actually have the controls on the page, but have them hidden until it is time to show them -- basically the Web 1.0 way of doing things. If you did the old style, then you would have references to all those controls as members of your form class.
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
 
Sorry. We are not a code writing service.

Maybe you can try using ChatGPT as your code monkey.
 
Back
Top Bottom