Using dynamically created controls in C#

2020-03-24 08:47发布

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?

标签: c# controls
5条回答
唯我独甜
2楼-- · 2020-03-24 09:11

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";
    }
}
查看更多
家丑人穷心不美
3楼-- · 2020-03-24 09:12

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:)

查看更多
做自己的国王
4楼-- · 2020-03-24 09:23

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"];
查看更多
干净又极端
5楼-- · 2020-03-24 09:25

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

查看更多
爷、活的狠高调
6楼-- · 2020-03-24 09:29

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();
    }
}
查看更多
登录 后发表回答