Adding controls to a form using generated code (VB6)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    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("V B.Label", "lblControl 1")
    lblcontrol1.Vis ible = True

    Dim WithEvents txtControl1 As TextBox
    Set txtControl1 = Controls.Add("V B.TextBox", "txtControl 1")
    txtControl1.Vis ible = 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
  • Hal Rosser

    #2
    Re: Adding controls to a form using generated code (VB6)

    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.Visibl e = True
    Command1.left = 100
    Command1.Top = 257
    etc

    "Chris" <chrisnm@mailin ator.com> wrote in message
    news:a0a9c4ef.0 408031106.72adb 00@posting.goog le.com...[color=blue]
    > 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("V B.Label", "lblControl 1")
    > lblcontrol1.Vis ible = True
    >
    > Dim WithEvents txtControl1 As TextBox
    > Set txtControl1 = Controls.Add("V B.TextBox", "txtControl 1")
    > txtControl1.Vis ible = 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[/color]


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


    Comment

    • J French

      #3
      Re: Adding controls to a form using generated code (VB6)

      On Tue, 3 Aug 2004 20:52:38 -0400, "Hal Rosser"
      <hmrosser@bells outh.net> wrote:
      [color=blue]
      >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
      >programaticall y (unless its part of a control array) - go figure.[/color]

      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

      Comment

      • Steve Gerrard

        #4
        Re: Adding controls to a form using generated code (VB6)


        "J French" <erewhon@nowher e.com> wrote in message
        news:41107ac2.7 5184850@news.bt click.com...
        | On Tue, 3 Aug 2004 20:52:38 -0400, "Hal Rosser"
        | <hmrosser@bells outh.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
        | >programaticall y (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...



        Comment

        • J French

          #5
          Re: Adding controls to a form using generated code (VB6)

          On Wed, 4 Aug 2004 19:31:54 -0700, "Steve Gerrard"
          <mynamehere@com cast.net> wrote:
          [color=blue]
          >
          >"J French" <erewhon@nowher e.com> wrote in message
          >news:41107ac2. 75184850@news.b tclick.com...
          >| On Tue, 3 Aug 2004 20:52:38 -0400, "Hal Rosser"
          >| <hmrosser@bells outh.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
          >| >programaticall y (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.[/color]

          Agreed
          [color=blue]
          >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.[/color]

          Yes, but I rather suspect that the OP does not want to ship the VB
          compiler to his users
          [color=blue]
          >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.[/color]

          And it would be a good idea to copy that structure
          - a way of 'double testing' during App development
          [color=blue]
          >A bit scary, but maybe fun to play around with...[/color]

          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

          Comment

          Working...