Solved

Dynamically Add / Load Usercontrol ControlArray in Runtime.

Posted on 2011-02-21
3
1,122 Views
Last Modified: 2012-05-11
Hello Experts,

I have a VB6 project in which I have the normal application (forms etc) and 1 usercontrols which I've developed.

I have added one instance of the usercontrol on the form as a control array and dynamically load and place multiple instances of it on the Form as and when required. This is all nice and smooth.

Now I have created a second user control within the project. This user control is essentially the same as the initial usercontrol, sharing the same properties and events. The only exception is the interface. I've made this small and compact - compared to the initial one, which is quite big.

On the application, I allow the users to set their preference on which version of the user control they'd like to see - the Standard or the Compact.

Based on the user preference, I should dynamically load the controls on screen.

On code, I use a control array and have name the control as Cabin1(x), so all manipulations, property changes are for Cabin1(x).

Now what I need to do is, Whatever control the user may select (Standard / Compact), I need that control to have the name Cabin1 and it should be created as a control array, so I can load multiple instances of it in Runtime and reference it as Cabin1(x).

How do I do it?
0
Comment
Question by:shaf81
3 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 34942926
If there were a way to place a generic "object" (i.e. Dim MyObject as Object) and have a control array of type "objects", that could take care of allowing you to use either control as the object.  But I don't know of a way to do that.

The only thing I can think of is to create a third control that simply holds one instance of BOTH of the other controls, and a flag that indicates which is the active control.  Have the excact same set of public functions, subroutines, and properties.  But everyone of these public members would be to check the flag to see which object is the active object and call the corresponding member of the active object.  (Of course the property that sets the flag would also make the desired control visible, and hide the other).
0
 
LVL 7

Accepted Solution

by:
kemi67 earned 500 total points
ID: 34943582
i've tested this solution and it works for an option button.
I have a form with two option buttons on array: Option1 and Option2
In the Form there is the index 0 of Option1 and Option2

Dim general As Variant
dim i as integer
i=0
If strUserChoise="Type 1" then
  Set  general = Option1
else
  Set  general = Option2
End If
i=i+1
Load general(i)
general(i).Visible=true


0
 
LVL 3

Author Comment

by:shaf81
ID: 34948980

kemi67: Genius. Works like a charm. Such a simple solution, and I never thought of that! Thank you.

HooKooDooKu: Good alternative suggestion. Thanks. However, I'd have to go with kemi67's answer for its simplicity.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

22 Experts available now in Live!

Get 1:1 Help Now