PDA

View Full Version : Create Copy of a Control



bbk5
03-21-2008, 04:53 PM
I'm dynamically creating a series of MSForms.Controls and wanted to know if there was any easy way for creating a copy of an existing control.

For example, I want to create a series of 24 Checkboxes based on data in an XML File. Once I create the first one after reading the XML file, my program may call it "SampleCheck1". I then want each subsequent Checkbox to just increase the number at the end of the name.

Now, is there anyway for me to copy all of the data stored in this Checkbox into a new one without looping through each individual attribute (such as Font, Size, etc.)? I figured I might be able to set the first Checkbox to a new Control variable, but I probably will still be referencing the original and not a copy as I'd hope.

Any advice on how to do this in a clean and efficient way? I'd appreciate it!

mikerickson
03-21-2008, 06:00 PM
If you are using this at design time, you can use the Designer object to create identical check boxes.

If this is happening at run time, creating controls on the fly is usualy not needed.

I'm not sure what you mean by "a series of 24 Checkboxes based on data in an XML File". What about the check boxes would be different depending on what is in the file?

bbk5
03-21-2008, 06:46 PM
Basically, the XML file is used to dynamically create a UserForm. This is to make it more extensible so that users just have to do some XML editing to get a UserForm to look the way they want it to.

So, I don't even know if I will have a CheckBox until runtime. The user also has the ability to say they want X copies of a Control they defined. I want to be able to read in the values from an XML file and create the first Checkbox. Then, depending on whether they specified they wanted more than 1, I can just create a copy of the first without having to use a loop to set all of the attributes of subsequent Controls.

Bob Phillips
03-22-2008, 02:47 AM
What about the code that is associated with the controls, how will you create that?

bbk5
03-22-2008, 12:09 PM
I'm using the actual UserForm controls already implemented in VBA. I'm just modifying their placement and other attributes via an XML file.

Bob Phillips
03-22-2008, 12:17 PM
How do you square that with '... I'm dynamically creating a series of MSForms.Controls'?

bbk5
03-22-2008, 03:04 PM
Because the controls are created at run-time, hence my use of the term dynamic. I don't know which ones I have to actually create until the XML file is read.

Bob Phillips
03-22-2008, 03:10 PM
So again I ask, for the controls created at run-time, how are you goig to create the event code associated with those controls created at run-time?

lucas
03-22-2008, 03:32 PM
I don't think he has thought that far ahead Bob. People who want to do this usually don't know what they are getting into.

I'm always surprised when people think this is a good idea.......I suppose there must be a use....Just because you can do something doesn't necessarily mean it's a good idea.

bbk5
03-22-2008, 05:26 PM
Actually, I have thought that far ahead. My application starts out with a standard multipage form. Then, the user can add tabs, populate them, etc. with a variety of Controls that I provide support for. I also have a variety of different events which can again be referenced in the XML file. There's no reason why a series of event handlers can't be tagged onto a Control that was created dynamically.

For the most part, my application just takes input from the user via a series of dynamic text and comboboxes. Then, when the user indicates they are done, I save this state and output to an Excel spreadsheet as defined by an output-formatting XML file.

Any other questions I can answer?

lucas
03-22-2008, 08:35 PM
Yes, if you have accomplished what you describe in post #10 then I would love to see a small working example. I'd be interested to see what you mean by:

I also have a variety of different events which can again be referenced in the XML file. There's no reason why a series of event handlers can't be tagged onto a Control that was created dynamically.

Maybe I'm missing something.....so far I don't understand why the complicated nature of your model is necessary.......I've got an open mind though.

bbk5
03-22-2008, 10:42 PM
The need for this model is that I want the user to be able to change the input/output of my program without needing to know anything about my VBA code. Sure, they'll need to understand some basic XML syntax, but hopefully nothing too drastic. As it is, there are 13 different form configurations and I've hardcoded in the basic controls. Whenever a new one is added, I have to go back and hardcode them in. Also, it is very difficult in the current state to make minor modifications to a hardcoded control without having the code become hard to maintain.

In terms of events, the majority are "disable this control when value x is selected" and so on. The basic event code is already in there, and through use of the XML file, the user can specify the parameters necessary for the event handler. If the user specifies they want the DisableEvent to be associated with a control, I'll just tie that event in at runtime and then keep track of the necessary parameters to execute the event.

Back to my original question, I was just wondering if there was an easy method for duplicating a given control. Like in the Design Editor, if you make a copy of a control, it just increments the number associated with the name. I was looking for a similar functionality in VBA to cut down on some possibly unnecessary code.

mikerickson
03-22-2008, 10:50 PM
It sounds like you want something like
(pseudo-code)
Call MakeOneCheckbox
Copy new checkbox
For i = 2 to numberOfCheckBoxes
Paste
next i

Have you considered this flow-chart as an option?
(pseudo-code)
For i = 1 to numberOfCheckBoxes
Call MakeOneCheckBox
Next i