-
May 7th, 2007, 01:41 PM
#1
Using a control
I've added a control to a form using
Code:
Controls.Add(new TextBox());
Now how do I actually use this textbox. How do I assign/read from it.
Thanks.
-
May 7th, 2007, 02:05 PM
#2
Re: Using a control
Easiest way is to have a variable:
Code:
TextBox myBox = new TextBox()
Controls.Add(myBox);
myBox.Text = "..";
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 7th, 2007, 02:09 PM
#3
Re: Using a control
Thanks. That makes sense. This .net is totally new, just trying to piece some stuff together.
-
May 7th, 2007, 03:00 PM
#4
Re: Using a control
Originally Posted by stin
I've added a control to a form using
Code:
Controls.Add(new TextBox());
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).
Still having said that, there may be times when you'll want to create anonymous objects.
-
May 7th, 2007, 04:15 PM
#5
Re: Using a control
its not really anonymous. its still stored in the Controls collection, but may be hard to find after you've put it there.
-
May 8th, 2007, 07:40 AM
#6
Re: Using a control
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.
-
May 8th, 2007, 08:03 AM
#7
Re: Using a control
Originally Posted by MadHatter
its not really anonymous. its still stored in the Controls collection, but may be hard to find after you've put it there.
I thought that it was anonymous since it doesn't have a variable name all its own. Here's my current thinking:
anonymous:
Code:
Controls.Add(new TextBox());
not-anonymous:
Code:
TextBox myNewTextBox = new TextBox();
Controls.Add(myNewTextBox);
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).
-
May 8th, 2007, 01:06 PM
#8
Re: Using a control
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
}
}
God could improve essentially a
human nature, but he
was too anxious with compatibility
with the monkey.
(Eugeny Goldberg)
-
May 8th, 2007, 02:18 PM
#9
Re: Using a control
Originally Posted by petes1234
I thought that it was anonymous since it doesn't have a variable name all its own. Here's my current thinking:
anonymous:
Code:
Controls.Add(new TextBox());
not-anonymous:
Code:
TextBox myNewTextBox = new TextBox();
Controls.Add(myNewTextBox);
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).
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)
Code:
new Form1().ShowDialog();
Sincerely,
Martin Svendsen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|