Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

adding controls at runtime

adding controls at runtime

adding controls at runtime

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.

Any help much appreciated - it's now 1:40 in the morning and I'm off to bed.....


RE: adding controls at runtime

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.

RE: adding controls at runtime

Did I miss something here? VB has long had this capability unless I've misread the question. From VB Books online,
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.
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.
Sample Application: Calc.vbp
End Paste_______________________________________________________

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.

Private Sub Form_Load()
Load Text1(1)
Load Text1(2)
Load Text1(3)
Text1(1).Left = Text1(0).Left
Text1(1).Top = Text1(0).Height + Text1(0).Top + 60
Text1(1).Visible = True
Text1(2).Left = Text1(1).Left
Text1(2).Top = Text1(1).Height + Text1(1).Top + 60
Text1(2).Visible = True
Text1(3).Left = Text1(2).Left
Text1(3).Top = Text1(2).Height + Text1(2).Top + 60
Text1(3).Visible = True
End Sub

RE: adding controls at runtime

Dear Databarn

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.
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!


RE: adding controls at runtime

Yeah, you're right, I should've specified a Text1(0) control on the form first.

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.

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.

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 <grin!>.
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 <grin!>.)

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.

Yikes, I've gotten long-winded! Sorry! Also, sorry I couldn't help you to resolve your conundrum.

RE: adding controls at runtime

Dear Databarn

Thanks for your last reply. Sorry I was so abrupt in my last message - shortages of time and chocolate. Never the best combination!

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.

If this command isn't implemented in VBA - any suggestions on how to *simply* achieve the same effect?

am now about to commit the cardinal sin of posting this message twice (God Bless Monty Python ....)


RE: adding controls at runtime

Ok, already, de-grovel <grin!>!

Nope, not an expert, just been playing with the beast since v1.0.

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.

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.

Will let you know one way or the other off the 'public' <grin> post.

(Monty who?!?) gdr...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close