Solved

Dynamically Add / Load Usercontrol ControlArray in Runtime.

Posted on 2011-02-21
3
1,126 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 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