Solved

(100 pts) Creating/destroying controls at run-time in VB6

Posted on 1999-01-18
6
320 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:HATCHET
6 Comments
 
LVL 4

Expert Comment

by:zsi
ID: 1469281
To do this, all you need to do is to create a single control on the form during design time and set it's index property = 0.  Then, during run time, do the following:

' 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>).<property>.  

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

0
 
LVL 3

Expert Comment

by:idt
ID: 1469282
Just to get my two cents in,

to destroy the created objects,

    For X = 1 To 10
       Unload Command1(X)
    Next

-iDT
0
 
LVL 15

Expert Comment

by:ameba
ID: 1469283
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.PVButton", "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

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Author Comment

by:HATCHET
ID: 1469284
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_Button.DragButton", "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

0
 
LVL 15

Accepted Solution

by:
ameba earned 100 total points
ID: 1469285
Thanks for the points.
By using this technique, I reduced memory requirements for my app dramatically.
Before I had 8 tabs with over 200 controls on my main form.
Now, I am dinamicaly loading/unloading 'pages' - my UserControls.
0
 
LVL 3

Author Comment

by:HATCHET
ID: 1469286
Adjusted points to 100
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now