-
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.
-
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.
-
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
-
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.
-
Gen-X, that's the way you do in VB5, because createobject works only in VB6
-
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
-
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
-
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
-
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
-
Try:
dim withevents tb as TextBox
set tb = Controls.Add("VB.TextBox", "text1")
Now you can access the properties and events