C# Get text from a textbox that is made at runtime

2019-06-14 04:08发布

问题:

Hello I am making a program that has 2 textboxes and 2 buttons When I press the add button then it will make 2 new textboxes using this code :

        private void ADD_ROW_Click(object sender, EventArgs e)
    {
        //Make the NEW_TEXTBOX_1
        HOW_FAR += 1;
        TextBox NEW_TEXTBOX_1 = new TextBox();
        NEW_TEXTBOX_1.Name = "NAME_TEXTBOX_" + HOW_FAR.ToString();

        //Set NEW_TEXTBOX_1 font
        NEW_TEXTBOX_1.Font = new Font("Segoe Print", 9);
        NEW_TEXTBOX_1.Font = new Font(NEW_TEXTBOX_1.Font, FontStyle.Bold);

        //Set pos and size and then create it.
        NEW_TEXTBOX_1.Location = new System.Drawing.Point(16, 71 + (35 * HOW_FAR));
        NEW_TEXTBOX_1.Size = new System.Drawing.Size(178, 29);
        this.Controls.Add(NEW_TEXTBOX_1);


        //Make the PRICE_TEXTBOX_
        TextBox NEW_TEXTBOX_2 = new TextBox();
        NEW_TEXTBOX_2.Name = "PRICE_TEXTBOX_" + HOW_FAR.ToString();

        //Set NEW_TEXTBOX font
        NEW_TEXTBOX_2.Font = new Font("Segoe Print", 9);
        NEW_TEXTBOX_2.Font = new Font(NEW_TEXTBOX_2.Font, FontStyle.Bold);

        //Set pos and size and then create it.
        NEW_TEXTBOX_2.Location = new System.Drawing.Point(200, 71 + (35 * HOW_FAR));
        NEW_TEXTBOX_2.Size = new System.Drawing.Size(89, 29);
        this.Controls.Add(NEW_TEXTBOX_2);

        //Change pos of the add button
        ADD_ROW.Location = new System.Drawing.Point(295, 71 + (35 * HOW_FAR));
        this.Height = 349 + (35 * HOW_FAR);
        this.Width = 352;
    }

This works very well but now I want to get the text from a newly made textbox back how do I do this?

This doesn't work because it says : NAME_TEXTBOX_1 Does not exist in the current context.

    private void button2_Click(object sender, EventArgs e)
    {
        string tmpStr = NAME_TEXTBOX_1.Text;
    }

回答1:

You need to move the variable declaration outside of the ADD_ROW_Click event handler so that it's accessible outside that block;

TextBox NEW_TEXTBOX_1;

private void ADD_ROW_Click(object sender, EventArgs e)
{
   //Make the NEW_TEXTBOX_1
   HOW_FAR += 1;
   NEW_TEXTBOX_1 = new TextBox(); //remove "TextBox" since we declared it above
   NEW_TEXTBOX_1.Name = "NAME_TEXTBOX_" + HOW_FAR.ToString();
  //...

The alternative, and possibly better depending on the number of textboxes, is to add each TextBox you create into a List. You can then iterate that List from and find the TextBox you want. For example

List<TextBox> allTextBoxes = new List<TextBox>();

private void ADD_ROW_Click(object sender, EventArgs e)
{
   //Make the NEW_TEXTBOX_1
   HOW_FAR += 1;
   TextBox NEW_TEXTBOX_1 = new TextBox(); 
   //...fill out the properties

   //add an identifier
   NEW_TEXTBOX_1.Tag = 1;

   allTextBoxes.Add(NEW_TEXTBOX_1);
 }

Then when you want a particular TextBox

private void button2_Click(object sender, EventArgs e)
{
    TextBox textBox1 = allTextBoxes.Where(x => x.Tag == 1).FirstOrDefault();
    string tmpStr = "";

    if(textBox1 != null)
       tmpStr = textBox1.Text;
}

Alternatively, and especially if you're going to have a lot of TextBoxes, you could store them in a Dictionary as Corak suggested in the comments.



回答2:

you're declaring NAME_TEXTBOX_1 within the ADD_ROW_Click method, which is why it isn't available within the button2_Cick method.

You can declare the textbox at the class level to access it in both places.

(You should work on renaming your variables too - e.g. TextBoxPrice)



回答3:

One simple solution: Make a private field called "NEW_TB" for example. In your button2_Click(..) { string tmpStr = NEW_TB.Text; } Add in your ADD_ROW_Click(..) method NEW_TB = NAME_TEXTBOX_1;

If I understood your question right, this should work.



回答4:

Make global your textboxes:

TextBox NEW_TEXTBOX_1;

then initiate them in your method:

NEW_TEXTBOX_1 = new TextBox();


回答5:

OMG Never mind sorry guys I found a good way :D

        var text = (TextBox)this.Controls.Find("PRICE_TEXTBOX_1", true)[0];
        text.Text = "PRO!";

This works pretty well :)