Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using dynamically created controls in C#

Tags:

c#

controls

I am creating an application where a user will input grades and the program will output the weighted average. On load, it will ask for the number of categories for the assignments. The program will then dynamically create textboxes for the user to input information. The problem is that I can not figure out how to read the text that is inputed after I create the textboxes. 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 I can find the current text in the box when the user submits?

like image 520
Franz Payer Avatar asked Jan 12 '11 04:01

Franz Payer


People also ask

How to access dynamically created controls in c#?

Solution 1 In this case since your creating them dynamically, you need to give them a unique name, then you can get the control from the control list by the name. Then search the control list for the name using ContainsKey(key) since the ControlCollection indexes the control Names as the key.

What are dynamic controls?

Dynamic control is a method to use model predictions to plan an optimized future trajectory for time-varying systems. It is often referred to as Model Predictive Control (MPC) or Dynamic Optimization.

How to add controls dynamically in asp net using c#?

Open your dynamic_demo. aspx, and drag and drop the table. Inside one of the td tags, you have to place checkboxlist, for prompting the user to what controls they want to generate.


2 Answers

If you are dynamically generating controls then obviously you won't be able to have a field for each one. But if you are trying to access the Controls collection for a named control, the ControlCollection can be indexed by name. After adding the text box with the specified name, you can simply do:

TextBox txtbx = (TextBox)Controls["txtbx1"];
like image 85
Josh Avatar answered Oct 12 '22 22:10

Josh


You could use the FindControl method of the Page class.

This method takes a parameter which is the TextBox's ID, which you have to set upon creation:

txtbx.ID = "txtbx1";

Then you can select it:

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

and use it.


Edit: Since the initial question added that he is refering to Windows Forms, my reply above is off-topic.

In Windows Forms, you should simply use a class member variable instead of a local variable. E.g.:

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";
    }
}
like image 22
Uwe Keim Avatar answered Oct 12 '22 22:10

Uwe Keim