CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Using a control

  1. #1
    Join Date
    Oct 2005
    Posts
    158

    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.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  3. #3
    Join Date
    Oct 2005
    Posts
    158

    Re: Using a control

    Thanks. That makes sense. This .net is totally new, just trying to piece some stuff together.

  4. #4
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Using a control

    Quote 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.

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  6. #6
    Join Date
    Oct 2005
    Posts
    158

    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.

  7. #7
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Using a control

    Quote 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).

  8. #8
    Join Date
    Mar 2004
    Location
    Ukraine
    Posts
    170

    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)

  9. #9
    Join Date
    Dec 2006
    Posts
    203

    Re: Using a control

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured