Results 1 to 2 of 2

Thread: Adding Controls at Runtime

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    North Texas
    Posts
    70

    Adding Controls at Runtime

    I need a little help with variable scope ( I think that is the right term)

    As I understand it, the follow could be used to add a button to my form at runtime ...

    Dim newButton As Button
    newButton = New Button()
    newButton.Location = ... etc...

    I think this defines a new object of type Button() called "newButton".

    Here is the quesiton ... Can "newButton" be a pointer to an object name?

    I want a method that creates a new button with the object name that I pass to the method.

    I am not sure how to do this.

    Thanks,
    David

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Controls at Runtime

    'newButton' is a reference to a Button object. That's what it is and that's all it can be. That Button object has a Name property that you can set and you can then use that name to retrieve a reference to that Button from the Controls collection of its Parent. For instance, if you do this:
    vb.net Code:
    1. Dim btn As New Button
    2.  
    3. btn.Name = "Clicker"
    4.  
    5. Me.Controls.Add(btn)
    then you can later do this:
    vb.net Code:
    1. Dim btn As Button = DirectCast(Me.Controls("Clicker"), Button)
    If that doesn't provide the information you need then you'll need to provide a clearer explanation of what you're trying to to achieve because this:
    Can "newButton" be a pointer to an object name?
    doesn't really make sense. There basically aren't any pointers in VB and, in languages that do support them, you would have a pointer to an object, not a pointer to the name of an object
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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