469,090 Members | 1,246 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,090 developers. It's quick & easy.

Adding controls to a form using generated code (VB6)

Dear all,

I would like to figure out a way to add controls to a form using specs
stored in a table. Any assistance in fleshing this idea out would be
greatly appreciated. I'm using VB6.

Right now I am thinking I would loop through the specs table
(containing control name, control type, width, max length, etc) and
print lines of code to a text file. The code would look something
like the following:

Dim lblcontrol1 As Label
Set lblcontrol1 = Controls.Add("VB.Label", "lblControl1")
lblcontrol1.Visible = True

Dim WithEvents txtControl1 As TextBox
Set txtControl1 = Controls.Add("VB.TextBox", "txtControl1")
txtControl1.Visible = True

The code generator would substitute the appropriate control names,
property settings, and so forth.

I would then need to run this code at design time to get the controls
on the form, but I'm not sure how to create a form and run this code.
I can put it in the Form_Load event, but then the controls are created
only at run time, and I would like to be able to save the controls on
the form.

Can anyone point me in the right direction?

Thanks,
Chris
Jul 17 '05 #1
4 12511
In VB6 - you you have to create a control array of each control type you
want to add at run time.(by setting the index property to 0) - you can set
the visible property to false if you don't want it to show. You can add
additional members of the control array - but you can't just add a control
programatically (unless its part of a control array) - go figure.
Then
load Command1(1)
load Command1(2)
Command1.Visible = True
Command1.left = 100
Command1.Top = 257
etc

"Chris" <ch*****@mailinator.com> wrote in message
news:a0*************************@posting.google.co m...
Dear all,

I would like to figure out a way to add controls to a form using specs
stored in a table. Any assistance in fleshing this idea out would be
greatly appreciated. I'm using VB6.

Right now I am thinking I would loop through the specs table
(containing control name, control type, width, max length, etc) and
print lines of code to a text file. The code would look something
like the following:

Dim lblcontrol1 As Label
Set lblcontrol1 = Controls.Add("VB.Label", "lblControl1")
lblcontrol1.Visible = True

Dim WithEvents txtControl1 As TextBox
Set txtControl1 = Controls.Add("VB.TextBox", "txtControl1")
txtControl1.Visible = True

The code generator would substitute the appropriate control names,
property settings, and so forth.

I would then need to run this code at design time to get the controls
on the form, but I'm not sure how to create a form and run this code.
I can put it in the Form_Load event, but then the controls are created
only at run time, and I would like to be able to save the controls on
the form.

Can anyone point me in the right direction?

Thanks,
Chris

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 7/29/2004
Jul 17 '05 #2
On Tue, 3 Aug 2004 20:52:38 -0400, "Hal Rosser"
<hm******@bellsouth.net> wrote:
In VB6 - you you have to create a control array of each control type you
want to add at run time.(by setting the index property to 0) - you can set
the visible property to false if you don't want it to show. You can add
additional members of the control array - but you can't just add a control
programatically (unless its part of a control array) - go figure.


I think you'll find that you can do it in VB6

But it is not much use

The Control Array approach is, I think the best
Jul 17 '05 #3

"J French" <er*****@nowhere.com> wrote in message
news:41***************@news.btclick.com...
| On Tue, 3 Aug 2004 20:52:38 -0400, "Hal Rosser"
| <hm******@bellsouth.net> wrote:
|
| >In VB6 - you you have to create a control array of each control type
you
| >want to add at run time.(by setting the index property to 0) - you
can set
| >the visible property to false if you don't want it to show. You can
add
| >additional members of the control array - but you can't just add a
control
| >programatically (unless its part of a control array) - go figure.
|
| I think you'll find that you can do it in VB6
|
| But it is not much use
|
| The Control Array approach is, I think the best

There wouldn't be much point in adding a control at runtime that wasn't
part of a control array, since you would have no event handlers for it
anyway.

If the OP really wants to do a code generator, OP could develop an
Add-In, which means learning the whole VBIDE object model and messing
with that. Or, OP could generate .frm files directly, then go to VB and
put them in a project.

A .frm file is just a text file, you can open several in NotePad to see
what they look like, and also the project.vbp file, to see how forms get
included in a project.

A bit scary, but maybe fun to play around with...

Jul 17 '05 #4
On Wed, 4 Aug 2004 19:31:54 -0700, "Steve Gerrard"
<my********@comcast.net> wrote:

"J French" <er*****@nowhere.com> wrote in message
news:41***************@news.btclick.com...
| On Tue, 3 Aug 2004 20:52:38 -0400, "Hal Rosser"
| <hm******@bellsouth.net> wrote:
|
| >In VB6 - you you have to create a control array of each control type
you
| >want to add at run time.(by setting the index property to 0) - you
can set
| >the visible property to false if you don't want it to show. You can
add
| >additional members of the control array - but you can't just add a
control
| >programatically (unless its part of a control array) - go figure.
|
| I think you'll find that you can do it in VB6
|
| But it is not much use
|
| The Control Array approach is, I think the best

There wouldn't be much point in adding a control at runtime that wasn't
part of a control array, since you would have no event handlers for it
anyway.
Agreed
If the OP really wants to do a code generator, OP could develop an
Add-In, which means learning the whole VBIDE object model and messing
with that. Or, OP could generate .frm files directly, then go to VB and
put them in a project.
Yes, but I rather suspect that the OP does not want to ship the VB
compiler to his users
A .frm file is just a text file, you can open several in NotePad to see
what they look like, and also the project.vbp file, to see how forms get
included in a project.
And it would be a good idea to copy that structure
- a way of 'double testing' during App development
A bit scary, but maybe fun to play around with...


While the OP would probably land up storing Control array parameters
in UDTs (for speed), it would be a good idea to write something that
generates a UDT from a Form and a Form from a UDT
- it would really show up errors
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Terry | last post: by
6 posts views Thread by Nathan Sokalski | last post: by
3 posts views Thread by Ankit Aneja | last post: by
1 post views Thread by npaulus | last post: by
1 post views Thread by srneu71 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.