Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding controls at runtime

Status
Not open for further replies.

wekkew

Programmer
Sep 28, 1998
123
GB
How do you add controls at run time? I've been struggling with trying to set up a collection. Even though I've done this and added new commandbuttons / textboxes how do I get the b**** things ONTO an actual form. Retreiving them from the collection by the item keyword works fine.<br>
<br>
Any help much appreciated - it's now 1:40 in the morning and I'm off to bed.....<br>
<br>
bye<br>

 
I've never done it, however, I read about it today in Visual Programmers journal. It's the special edition for VB6 (you can't do it in VB5 by the way). Lots of good reading about new features in VB6.
 
Did I miss something here? VB has long had this capability unless I've misread the question. From VB Books online,<br>
Paste___________________________________________________<br>
If you want to create a new control at run time, that control must be a member of a control array. With a control array, each new element inherits the common event procedures of the array.<br>
Without the control array mechanism, creating new controls at run time is not possible, because a completely new control would not have any event procedures. Control arrays solve this problem, because each new control inherits the common event procedures already written for the array. For example, if your form has several text boxes that each receive a date value, a control array can be set up so that all of the text boxes share the same validation code.<br>
Sample Application: Calc.vbp<br>
End Paste_______________________________________________________<br>
<br>
Also, see "Adding to a Control Array at Runtime" in the VB Books Online. However, the code below can be pasted into a new form and run, it should provide what I _think_ you're asking.<br>
<br>
Private Sub Form_Load()<br>
Me.Show<br>
Load Text1(1)<br>
Load Text1(2)<br>
Load Text1(3)<br>
Text1(1).Left = Text1(0).Left<br>
Text1(1).Top = Text1(0).Height + Text1(0).Top + 60<br>
Text1(1).Visible = True<br>
Text1(2).Left = Text1(1).Left<br>
Text1(2).Top = Text1(1).Height + Text1(1).Top + 60<br>
Text1(2).Visible = True<br>
Text1(3).Left = Text1(2).Left<br>
Text1(3).Top = Text1(2).Height + Text1(2).Top + 60<br>
Text1(3).Visible = True<br>
End Sub<br>
<br>

 
Dear Databarn<br>
<br>
<br>
Obviously you need to have at least the zero-ith(!) element of a control array on your form before you can add to it. Collections don't require this and so are a better option. After all, since Microsoft went to all that trouble of coining that new phrase "object based(ha bloody ha)" I thought I'd give it a bash. I don't know if you've done any VBA programming but collections are often the only way you have of referring to/changing built in objects.<br>
What is driving me to the fridge in desperation is the fact that although the collection has been setup - I can't get the items onto a form!<br>
<br>
bye<br>

 
Yeah, you're right, I should've specified a Text1(0) control on the form first.<br>
<br>
Sorry about the offtrack - I read that you wanted to add controls, not that you wanted to specifically use a collection for the purpose - my dumb.<br>
<br>
Problem with collections, at least in the 5.0 iteration of VB, is that they're pretty much either form or database related. You can work with an existing collection of controls, but you cannot create a _new_ collection of such(?!?). At least, that's what RTFM tells me.<br>
<br>
Theoretically, your statement ab. collections being a better option is correct, but in all practicality, that's not so much the case. First of all, the noted lack of creating the collection of choice comes to mind &lt;grin!&gt;. <br>
But, then, there is the not-so-obvious overhead trade-off to be considered. Which 'costs' more, the 'seed' control loading, or the creation time of the collection initialization? Can't speak to VB5 on this, but in VB3 and VB4, using a forms collection was always slow, so I quit doing it - but then, that could have been my implementation rather than any fault of the collections. (OK, begging the question for VB3, but VB4 had 'em &lt;grin!&gt;.)<br>
<br>
Getting back to that 'collection of choice' mentioned above, VB's limited capability in the collection area (i.e., creating new ones) is kinda frustrating (But you know that, no?), and pretty much self-flagellation when trying to step outside the limited examples (and instruction/advisory of the documentation). Since I'm pretty much restricted to the real (read 'practical'?) world, I've reverted to a proven, albeit older, technology.<br>
<br>
Yikes, I've gotten long-winded! Sorry! Also, sorry I couldn't help you to resolve your conundrum.<br>

 
Dear Databarn<br>
<br>
Thanks for your last reply. Sorry I was so abrupt in my last message - shortages of time and chocolate. Never the best combination!<br>
<br>
As you seem such an expert (grovel...) here's another that I hope you can help with. Am I right in assuming there is no POPUPMENU command in VBA? It doesn't feature in the object browser under Word / Excel etc.<br>
<br>
If this command isn't implemented in VBA - any suggestions on how to *simply* achieve the same effect?<br>
<br>
am now about to commit the cardinal sin of posting this message twice (God Bless Monty Python ....)<br>
<br>
byee<br>
Kate<br>

 
Ok, already, de-grovel &lt;grin!&gt;!<br>
<br>
Nope, not an expert, just been playing with the beast since v1.0.<br>
<br>
As to your question - and sin! - I don't _think_ you can do a popup with VBA, and I fail to see the sin in this supposed double post - one is to the world at large, and the other was to a specific individual - not quite the same thing as cross-list posting. <br>
<br>
Howver, VBA is not a tool I've used extensively, as most of my stuff is database and report driven. However, I seem to recall doing something like this with Word Basic a number of years back. I'll see if I can dig up that macro, think a few folk at work are still using it in an old Word-based reporting tool.<br>
<br>
Will let you know one way or the other off the 'public' &lt;grin&gt; post.<br>
<br>
(Monty who?!?) gdr...<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top