Results 1 to 7 of 7

Thread: Creating Controls at Run-time

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Manchester, UK
    Posts
    50

    Cool

    Does anyone know if it is possible to create a certain number of shape objects whilst the program is running? What I want to do is create a certain number of rectangles depending on a variable in my program.

  2. #2
    Guest
    Make a CommandButton and put this code in it.

    Code:
    Private Sub Command1_Click()
    
    Controls.Add "VB.Shape", "Shape1"
    
       Me!Shape1.Move 0, 0
       Me!Shape1.Visible = True
    
    End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Manchester, UK
    Posts
    50

    Angry

    That didn't work, I get an error saying "Object doesn't support that property or method". Does that work for you cos I am using VB5 SP3 aswell?

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yeah, that's the big deal with vb6, i have vb5 and that doesn't work for me either. You must use a an array of objects and use Load object(index) to load a second item. There's a lot of threads on this topic, go search for "create runtime"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    New Member
    Join Date
    Jun 2000
    Location
    DC area
    Posts
    2

    Thumbs up

    In VB6 I believe you would go about this using a control array. Try it in VB5 and see if this works.

    Your form has a control Shape1. In the IDE, set it's Index property to 0. This tells VB it is the first member in a control array.

    Code:
    Dim intNumber as Integer
    
    'intNumber can be any integer value
    intNumber = 6
    
    'Enter your loop
    For i = 1 To intNumber
    'Load a new instance of Shape1 into the control array
        Load Shape1(i)
    'The default setting of each new controls Visible 
    'property is False.  Change it or you won't see them
        Shape1(i).Visible = True
    
    'Remember not to overlap them unless you want to
    'This spaces the controls out vertically with 20 twips or
    'or pixels whatever between them.  You can do the same
    'vertically by using Top instead of Left
        Shape1(i).Left = Shape1(i - 1).Left + Shape1(i).Width + 20
    Next
    I hope this helps But as I said this something I've done in VB6, I don't recall if VB5 would do the same thing in the same fashion. Give it a shot.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Manchester, UK
    Posts
    50

    Cool Thanks

    Cheers very much for that, it does work in VB5.

  7. #7
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    This should work:
    Code:
    Dim Kontroll As Control
    Dim ActiveForm As Object
    Set ActiveForm = Form1
    Set Kontroll = ActiveForm.Controls.Add("Forms.TextBox.1", "SomeNameOnTheControl)
    Kontroll.left = 10
    Kontroll.top = 10
    Kontroll.width = 100
    Kontroll.height = 16
    Kontroll.Text = "SomeText"
    
    etc...
    Done this is VBA for Office 97 which is somewhere between VB 4 and VB 5.

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