Results 1 to 10 of 10

Thread: Creating new objects during run-time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Posts
    17
    I know there's a way but I just can't figure it out. I'm trying to create a new instance of a control during run-time. For example, if someone clicked a button, VB would create a new textbox, same way that it would do it in design-time, only now it would do it in runtime. Thanks.

  2. #2
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    You might find the CreateObject function useful for this. Here is what (part of) the VB documentation says:

    Syntax

    CreateObject
    (class)

    The class argument uses the syntax appname.objecttype and has these parts:

    appname -- Required; Variant (String). The name of the application providing the object.

    objecttype -- Required; Variant (String). The type or class of object to create.

    This info can be found in Visual Books Online.

  3. #3
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    Something like:
    Code:
    Dim Object As Object
    Set Object = CreateObject(object.object)
    Object.Visible = True
    That looks confusing...hope you understand it.

    Laterz,
    D!m
    Dim

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    I dont think that works for controls on forms like textboxes

    I think what the boy needs is something like this :

    1. First you have to have a textbox on the form
    2. Second you need to make it a control array
    3. You need to do a "Load txtBox(1)"

    You can then continue to "Load" with the new index number and then set its Left and Top properties to position it elsewhere on the form.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Gen-X, that's the way you do in VB5, because createobject works only in VB6
    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.

  6. #6
    Junior Member
    Join Date
    Jul 2000
    Posts
    19
    this is really easy to do..Make a text box..set its Index to 1, then in a command button.. put this code

    ' dim the text(index) count
    Dim ti
    ' set the text(index) count and add one..to create the new control
    ti = Text1.Count + 1
    ' load a new control with our index (ti)
    Load Text1(ti)
    ' set the text(index) options
    Text1(ti).Text = "thanks jier! " & ti
    Text1(ti).Visible = True

    'this code will stack the boxes. make a big form..press your button like 50 times..lol
    Text1(ti).Top = Text1(ti-1).Top + Text1(ti-1).Height

  7. #7
    Guest
    If you have VB6, try this.

    Code:
    Private Sub Command1_Click()
    
        Controls.Add "VB.CommandButton", "Command2"
        Me!Command2.Move 0, 0
        Me!Command2.Caption = "New Button"
        Me!Command2.Visible = True
    
    End Sub

  8. #8
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610
    My apologies for resurrecting an old thread. Once you dynamically create the new command button can you dynamically create the click event code for this button?

    Thanks
    This space for rent...

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That depends.. If you have an array of controls, loading the next one, you could use the same event where the index argument tells you which control is firing your event
    otherways, there's no way but subclassing
    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.

  10. #10
    New Member
    Join Date
    Jul 2000
    Posts
    10
    Try:

    dim withevents tb as TextBox
    set tb = Controls.Add("VB.TextBox", "text1")


    Now you can access the properties and events

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