I've added a control to a form using
Now how do I actually use this textbox. How do I assign/read from it.Code:Controls.Add(new TextBox());
Thanks.
Printable View
I've added a control to a form using
Now how do I actually use this textbox. How do I assign/read from it.Code:Controls.Add(new TextBox());
Thanks.
Easiest way is to have a variable:
Code:TextBox myBox = new TextBox()
Controls.Add(myBox);
myBox.Text = "..";
Thanks. That makes sense. This .net is totally new, just trying to piece some stuff together.
What you were doing here was creating an object w/out putting it into a reference variable which is known as an anonymous object. The object (TextBox) exists, but since it was created w/out a variable name (as TheCPUWizard showed you how to do correctly in the next post) there was no way to get to it, to use it in your code. (this is not strictly 100% true as you could potentially get to it through the Controls collection, but it wouldn't be as easy or intuitive).Quote:
Originally Posted by stin
Still having said that, there may be times when you'll want to create anonymous objects.
its not really anonymous. its still stored in the Controls collection, but may be hard to find after you've put it there.
I had originally tried looking for a method in the Controls collection but all I saw were methods for getting by hash id and the like. I never saw a way to set it though.
Thanks for you help folks.
I thought that it was anonymous since it doesn't have a variable name all its own. Here's my current thinking:Quote:
Originally Posted by MadHatter
anonymous:
not-anonymous:Code:Controls.Add(new TextBox());
Please correct me if my concepts are wrong (it's happened once or twice that I can recall -- unless you ask my wife; according to her, it happens all the time).Code:TextBox myNewTextBox = new TextBox();
Controls.Add(myNewTextBox);
Well but anonymus control can be find by name=="" so you may do so
Code:Form1.Controls.Add(new textbox());
foreach(textbox box in Form1.Controls)
{
if(box.Name=="")//you may check other params
{
//Do you work with object here it`s our new created object
}
}
I see neither of those as anonymous, really, since you're just adding it to a collection in the first case. An anonymous object would be like (from my understanding)Quote:
Originally Posted by petes1234
Code:new Form1().ShowDialog();