Using dynamically created controls in C #

I am creating an application in which the user will enter estimates, and the program will output a weighted average value. When loading, it will ask for the number of categories for tasks. Then the program will dynamically create text fields for user input. The problem is that I cannot figure out how to read the text that is entered after creating the text fields. Here is my code:

TextBox txtbx = new TextBox(); txtbx.Text = ""; txtbx.Name = "txtbx1"; txtbx.Location = new Point(10, 10); txtbx.Height = 20; txtbx.Width = 50; Controls.Add(txtbx); 

How can I change this code so that I can find the current text in the field when the user submits?

+4
source share
5 answers

If you dynamically generate controls, then obviously you cannot have a field for each of them. But if you are trying to access the Controls collection for a named control, the ControlCollection can be indexed by name. After adding a text field with the specified name, you can simply:

 TextBox txtbx = (TextBox)Controls["txtbx1"]; 
+11
source

You can use the FindControl method of the Page class.

This method takes a parameter, which is the TextBox identifier that you must set when creating:

 txtbx.ID = "txtbx1"; 

Then you can select it:

 TextBox txtbx1 = (TextBox)FindControl("txtbx1"); 

and use it.


Edit: Since the initial question added that it refers to Windows Forms, my answer above is off topic.

On Windows Forms, you should simply use a member variable of the class instead of a local variable. For instance:.

 public partial class MyForm { ... private TextBox txtbx; ... private void createControls() { txtbx = new TextBox(); txtbx.Text = ""; txtbx.Name = "txtbx1"; txtbx.Location = new Point(10, 10); txtbx.Height = 20; txtbx.Width = 50; Controls.Add(txtbx); } private void someOtherFunction() { // Do something other with the created text box. txtbx.Text = "abc"; } } 
+4
source

Save the list of links to all text fields in the form. Add a dynamic binding of textBox to the list when creating them dynamically.

Then you can simply iterate over all the text fields in the list when you want to read their text.

Make sure you write the text fields according to their related category names. Then you can also find the control in the list by their names.

 class MyForm : Form { IList<TextBox> _textBoxes = new List<TextBox>(); private void AddTextBox(string categoryName){ var myTextBox = new TextBox(); myTextBox .Name = categoryName + "txtbx"; // set other properties and add to Form.Controls collection _textBoxes.Add(myTextBox); } private TextBox FindTextBox(string categoryName) { return _textBoxes.Where( t => t.Name.StartsWith(categoryName)).FirstOrDefault(); } } 
+1
source

This code to add a dynamic add text box Click

 int count = 1; public System.Windows.Forms.TextBox AddNewTextBox() { System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox(); this.Controls.Add(txt); txt.Top = count * 25; txt.Left = 100; txt.Text = "TextBox " + this.count.ToString(); count = count + 1; return txt; } private void Onbutton_Click(object sender, EventArgs e) { //Call the method AddNewTextBox that uses for Dynamically create Textbox AddNewTextBox(); } 

Hope this code helps you. Thank you. Happy coding :)

+1
source

All you have to do is set up the OnClick listener for the submit button and do something like this

 private void OnSubmit(object sender, EventArgs args) { string yourText = txtbx.Text; } 

You will need to leave a link to the text box after creating it. yourText will contain the desired value. Hope this helps

0
source

Source: https://habr.com/ru/post/1335195/


All Articles