[RESOLVED] Loading a new control onto form
Now I *know* how to load a new control if I already have an instance of that control on the form...question is can I load a control onto a form if there isn't already one. For instance, I made that tab thing that shows tabs like in FireFox and places them equally at the top of the screen. I am now working on unloading the tabs, but I can't unload the first tab as it was added at design time thus the error is "can't unload controls created at design time"
My question basically is HOW do I create the first object in a control array at runtime, and is it possible? If not, I know I can just hide array object (0) and work from that :-)
Re: Loading a new control onto form
You may use Controls collection:
VB Code:
Dim txt As Textbox 'or declare it as Control
Set txt = Me.Controls.Add("VB.Textbox", "txtTemp")
With txt
.Text = "Hi. I am new here"
.Move 300, 500 'or whatever plus you may set Height/Width as well
.Visible = True
End With
'to delete control that was added using controls collection use Remove method:
Me.Controls.Remove "txtTemp"
NOTE: technic from the above sample will require lisense for any non intrinsic control (Intrinsic controls are those that are built into VB and always appear as default controls set: command button, textbox, etc, etc, etc...).
Re: Loading a new control onto form
You can also declare the object WithEvents and then access all the events associated with an object of that type:
VB Code:
Private WithEvents txt As Textbox
Private Sub txt_Change()
End Sub
Re: Loading a new control onto form
In VB6 adding control through controls collection and also declaring it using WithEvents keyword isn't as flexible as it may look and that is the reason I've decided not to use it in my sample.
But it is important to know that this feature exist. :thumb:
Re: Loading a new control onto form
Rhino's code seems to work okay...I'm having problems trying to convert the OptionButton to graphical though, and I get the feeling I might have to rewrite whole blocks of code to get this to work with the other stuff I did. I might just hide (0) and use 1+ :-)
Also, using the WithEvents, I am assuming I can't use a control array like I need to, or if I can it won't be as flexible as I'd like it to be
Re: Loading a new control onto form
Quote:
Originally Posted by RhinoBull
In VB6 adding control through controls collection and also declaring it using WithEvents keyword isn't as flexible as it may look and that is the reason I've decided not to use it in my sample.
But it is important to know that this feature exist. :thumb:
I mentioned it only for completeness and the fact that some dynamic controls would be pointless without it.
Re: Loading a new control onto form
Quote:
Originally Posted by smUX
...Also, using the WithEvents, I am assuming I can't use a control array like I need to, or if I can it won't be as flexible as I'd like it to be
No, you cannot create control array using the Add method - only Load allows this and at least one element MUST be created at design time.
Re: Loading a new control onto form
Quote:
Originally Posted by RhinoBull
No, you cannot create control array using the Add method - only Load allows this and at least one element MUST be created at design time.
Too bad...Guess I'll stick with the original jerry-rigging I am so good at :-)
I'm chucking (0) away where it can't be seen and writing it so the program loads 1-2 on runtime and works from there :-)
Currently I have it adding array items and deleting them dynamically (so it can delete one in the middle and when you add a new one it takes up a free space that was made by deleting one)
I think my next project for this is multi-row tabs :-)
Re: Loading a new control onto form
Well bugger me sideways, it works :-)
I've attached the most recent version...clicking tabs now deletes them (just to show that they can be deleted) and as I said above, they are dynamically reassigned new index numbers so there shouldn't be any problems with clashing indexes or anything :-)
Re: [RESOLVED] Loading a new control onto form
Rhino, you mention non-intrinsic controls requiring something extra if I wanted to load new instances of them...exactly what do I do?
Re: [RESOLVED] Loading a new control onto form
For each non-intrinsic control you will have to add a license.
Look up MSDN for Licenses Collection but in a nutshell it's painfull and not a good idea.
Better approach (by far) is to add control to your form in design and use control array if you need more than one instance.
Re: [RESOLVED] Loading a new control onto form
Yeah, I'm doing that...I thought that was what I would need for this too...I have a webbrowser (yeah, trying to copy firefox :-)) and I am using load wb(x) (wb is the name I am using, and x is 1 in this case...there's a wb(0) already available)
However, the error is "Property let procedure not defined and property get procedure did not return an object"
Edit: Ignore...showing my noobishness...forgot to make the original index 0 :-)