Results 1 to 6 of 6

Thread: Dynamic Control Creation

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Dynamic Control Creation

    Hi all, i am trying to add a simple label to my form at run-time, i am experimenting with some code and i am wondering whether this is correct as it is not working...
    (VB6)
    VB Code:
    1. Private Sub Command1_Click()
    2. Me.Controls.Add "VB.Label", "Label1"
    3. Me!Label1.Caption = "New Label Created At runtime"
    4. Me!Label1.Visible = True
    5. Me!Label1.Left = 100
    6. Me!Label1.Top = 100
    7. Me!Label1.Height = 25
    8. Me!Label1.Width = 150
    9. End Sub

    Where am i going wrong ?
    Zeegnahtuer?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Dynamic Control Creation

    The first issue was the use of ! instead of .

    The next issue is that the control doesnt exist when the sub starts, so Me.Label1 would be invalid anyway. To get around this you need to use the controls collection, like this:

    Me.Controls("Label1").Caption = "New Label Created At runtime"

    In this situation I would recomnend using a With block to cut down the code, eg:
    VB Code:
    1. Me.Controls.Add "VB.Label", "Label1"
    2.  
    3. With Me.Controls("Label1")
    4.   .Caption = "New Label Created At runtime"
    5.   .Visible = True
    6.   .Left = 100
    7.   .Top = 100
    8.   .Height = 25
    9.   .Width = 150
    10. End With

    An alternative method to create new controls at runtime is to create one at design time, and set its Index property to 0 (so it is part of a control array), and it's Visible property to false (so it wont be shown).

    Then at run time you can run code like this (if it is called myLabel):
    VB Code:
    1. Load myLabel(1)
    2. myLabel(1).Caption = "Hello"
    3. myLabel(1).Visible = True
    This method has the advantage that you can write code for the events at design time (for the "hidden" one, using the Index parameter), without having to know how many new controls will be added.

  3. #3
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Dynamic Control Creation

    Quote Originally Posted by thegreatone
    Hi all, i am trying to add a simple label to my form at run-time, i am experimenting with some code and i am wondering whether this is correct as it is not working...
    (VB6)
    VB Code:
    1. Private Sub Command1_Click()
    2. Me.Controls.Add "VB.Label", "Label1"
    3. Me!Label1.Caption = "New Label Created At runtime"
    4. Me!Label1.Visible = True
    5. Me!Label1.Left = 100
    6. Me!Label1.Top = 100
    7. Me!Label1.Height = 25
    8. Me!Label1.Width = 150
    9. End Sub

    Where am i going wrong ?

    It seems to be a problem with this bit:

    VB Code:
    1. Me!Label1.Height = 25
    2. Me!Label1.Width = 150

    When I changed that too:

    VB Code:
    1. Me.Controls.Add "VB.Label", "Label1"
    2. Me!Label1.Caption = "New Label Created At runtime"
    3. Me!Label1.Visible = True
    4. Me!Label1.Left = 100
    5. Me!Label1.Top = 100
    6. Me!Label1.Height = Form1.Height
    7. Me!Label1.Width = Form1.Width

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Dynamic Control Creation

    thegreatone,

    Take a look at Dynamic Control Loading

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Dynamic Control Creation

    Here is a tutorial written by NoteMe : http://www.developerkb.com/modules/w...p?articleid=35


    Has someone helped you? Then you can Rate their helpful post.

  6. #6

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