Franz Payer
Franz Payer

Reputation: 4137

Using dynamically created controls in C#

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?

Upvotes: 5

Views: 28543

Answers (5)

Dipanki Jadav
Dipanki Jadav

Reputation: 390

This code for the Dynamically Add Textbox On Button 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();
    }

I hope this code will help you . Thank You Happy Coding:)

Upvotes: 3

Unmesh Kondolikar
Unmesh Kondolikar

Reputation: 9312

Keep a list of references of all text boxes on the form. Add the textBox reference to the list when you create them dynamically.

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

Make sure that you name the text boxes as per 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();
    }
}

Upvotes: 1

Josh
Josh

Reputation: 69262

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"];

Upvotes: 16

Uwe Keim
Uwe Keim

Reputation: 40736

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";
    }
}

Upvotes: 3

rchanley
rchanley

Reputation: 459

All you need to do is set up an OnClick listener for your submit button and have it do something like this

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

You'll have to keep a reference to the text box after you create it. yourText will contain the value you need. Hope this helps

Upvotes: 0

Related Questions