HATCHET
asked on
(100 pts) Creating/destroying controls at run-time in VB6
I am using VB6 but also have VB5... I can use either for this.
I have an ActiveX control that I created called "DragButton" which is basically a beefed-up CommandButton. At run-time, I need to create this control so I don't have to put a bunch of these buttons on the form and hide/show them when I need a new them. I'd like to just create one when I need it and destroy it when I no longer need it.
How can I do this? Please provide some sample code and a brief explaination. I'll award 100 points if you can help on this one.
Thank you,
HATCHET
I have an ActiveX control that I created called "DragButton" which is basically a beefed-up CommandButton. At run-time, I need to create this control so I don't have to put a bunch of these buttons on the form and hide/show them when I need a new them. I'd like to just create one when I need it and destroy it when I no longer need it.
How can I do this? Please provide some sample code and a brief explaination. I'll award 100 points if you can help on this one.
Thank you,
HATCHET
Just to get my two cents in,
to destroy the created objects,
For X = 1 To 10
Unload Command1(X)
Next
-iDT
to destroy the created objects,
For X = 1 To 10
Unload Command1(X)
Next
-iDT
If your OCX's title is 'PV', and your UserControl is 'PVButton', in VB6 use this:
Private oControl As Control
For ctlno = 1 to 10
Set oControl = frm.Controls.Add("PV.PVBut ton", "Button" & ctlno)
oControl.Move 0, sumy
oControl.Visible = True
Next
To remove control
Set oControl = frm.Controls("Button1")
frm.Controls.Remove oControl
Set oControl = Nothing
Private oControl As Control
For ctlno = 1 to 10
Set oControl = frm.Controls.Add("PV.PVBut
oControl.Move 0, sumy
oControl.Visible = True
Next
To remove control
Set oControl = frm.Controls("Button1")
frm.Controls.Remove oControl
Set oControl = Nothing
ASKER
zsi,
I know how to use Control Arrays, but I don't think it's what I want to do on my project, so your answer isn't relevent. Thanks for trying to help.
idt,
Thanks for the $0.02 =]
ameba,
You hit the nail RIGHT on the head!! Thank you so much! Exactly what I was looking for. If you could, please answer this question and I'll award you the points you deserve for the help. =]
HATCHET
========================== ========== ========== ========== =========
Below is the code I used to test the code :
========================== ========== ========== ========== =========
- Start a new project
- Add the ActiveX Control "Drag_Button" to the project
( * NOTE: From this point on, substitute whatever the name of your OCX is for "Drag_Button")
( * NOTE: From this point on, if you substitue "VB" for "Drag_Button", and substitute the name of the standard VB control for "DragButton" if you are using a standard VB Control like a CommandButton)
- Add 2 Command Buttons to Form1
- Insert the following code into the code window of Form1
__________________________ __________ __________ __________ __________ _______
Option Explicit
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private MyControl As Control
Private Sub Command1_Click()
Dim MyCounter As Integer
Dim TheCaption As String
TheCaption = Me.Caption
Me.Caption = " Please Wait..."
For MyCounter = 1 To 10
Set MyControl = Me.Controls.Add("Drag_Butt on.DragBut ton", "Button" & MyCounter)
MyControl.Move 0, (MyCounter * (MyControl.Height + 15))
MyControl.Visible = True
DoEvents
Next
Me.Caption = TheCaption
End Sub
Private Sub Command2_Click()
Dim MyCounter As Integer
For MyCounter = 1 To 10
Set MyControl = Me.Controls("Button" & CStr(MyCounter))
MyControl.Caption = "Bye Bye"
DoEvents
Sleep 500
Me.Controls.Remove MyControl
Set MyControl = Nothing
Next
End Sub
I know how to use Control Arrays, but I don't think it's what I want to do on my project, so your answer isn't relevent. Thanks for trying to help.
idt,
Thanks for the $0.02 =]
ameba,
You hit the nail RIGHT on the head!! Thank you so much! Exactly what I was looking for. If you could, please answer this question and I'll award you the points you deserve for the help. =]
HATCHET
==========================
Below is the code I used to test the code :
==========================
- Start a new project
- Add the ActiveX Control "Drag_Button" to the project
( * NOTE: From this point on, substitute whatever the name of your OCX is for "Drag_Button")
( * NOTE: From this point on, if you substitue "VB" for "Drag_Button", and substitute the name of the standard VB control for "DragButton" if you are using a standard VB Control like a CommandButton)
- Add 2 Command Buttons to Form1
- Insert the following code into the code window of Form1
__________________________
Option Explicit
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private MyControl As Control
Private Sub Command1_Click()
Dim MyCounter As Integer
Dim TheCaption As String
TheCaption = Me.Caption
Me.Caption = " Please Wait..."
For MyCounter = 1 To 10
Set MyControl = Me.Controls.Add("Drag_Butt
MyControl.Move 0, (MyCounter * (MyControl.Height + 15))
MyControl.Visible = True
DoEvents
Next
Me.Caption = TheCaption
End Sub
Private Sub Command2_Click()
Dim MyCounter As Integer
For MyCounter = 1 To 10
Set MyControl = Me.Controls("Button" & CStr(MyCounter))
MyControl.Caption = "Bye Bye"
DoEvents
Sleep 500
Me.Controls.Remove MyControl
Set MyControl = Nothing
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adjusted points to 100
' assuming the control is named Command1
' Let's create 10 controls.
for x = 1 to 10
Load Command1(x)
Command1(x).visible = true
next
What you are doing here is creating a control array. You can access any of the properties of the individual controls by referring to their index value like this: Command1(<index>).<propert
When you run this code, the controls will appear directly on top of one another. Therefore, you will want to move the controls to their desired locations.
For more information, look up "Control Arrays" in the VB documentation
Hope this helps.
zsi