Hi, I am creating a program for my business that allows us to write quotes quickly, I am fairly new to C# and am stuck on a particular matter.

i cannot relate to my Dynamically created textbox no matter what i name it, i am using the following code to create the textbox.

TextBox field2 = new TextBox();
            field2.Location = new Point(15, 85);
            field2.Size = new Size(185, 20);
            field2.Name = "textBox2";
            field2.Visible = true;
            this.Controls.Add(field2);

Whenever i relate to field2 i get the error that field2 does not exist despite being created when i click the button in debug mode.

I merly want to relate to it in a button so i can store the text in a variable and need help please.

As I understand correctly - you're trying to access dynamically created variable field2 outside the borders of it's visibility, rather than TextBox.

To locate your dynamically created TextBox you should use this:

this.Controls["textBox2"].Text = "I'm here";

or this:

//this method returns Control[], so you should specify the index
//of interested control in that array
this.Controls.Find("textBox2", false)[0].Text = "And here";

Thank you so much, that was just what I wanted.

You're welcome .. if your issue is solved - please mark this thread as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.