Dynamically create controls/components at runtime
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Dynamically create controls/components at runtime

  1. #1
    Join Date
    May 1999
    Posts
    9

    Dynamically create controls/components at runtime

    Dear reader,

    I am trying to create controls within a form during runtime (depending on external ini file).
    I want to use labes and/or other ocx controls like msflexgrid control.

    Now if I place a msflexgrid directly within the form while developing, I have only one
    instance of it in one form which works fine.
    Using an undetermined amount of tab pages created at runtime, each of this tabpages
    should contain it's own control. How do I now _create_ a NEW msflexgrid control?

    My first idea:
    dim grid as msflexgrid (doesnt work for me)

    Further I encounterd the create_object() function, but I dont know the classname
    of all the controls I use, and a
    create_object("msflexgrid") or
    create_object("msflxgrd.ocx")
    fails.

    The control IS added to the project (since I use it in another form).
    Any hints?
    Uwe Reimann


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Dynamically create controls/components at runtime

    there are two ways to go:
    1. create one grid at designtime and give it an Index of 0
    then, at runtime, do a

    for i = 1 to 5
    load mygrid(i)
    mygrid(i).visible = true ' necessary!
    mygrid.top... place it wherever you want
    next i




    2. use the Controls.Add method to create ALL of your grids at runtime (VB 6 only).


  3. #3
    Join Date
    May 1999
    Posts
    9

    Re: Dynamically create controls/components at runtime

    >2. use the Controls.Add method to create ALL of your grids at runtime (VB 6 only).
    Hmm, I use VB6 so this is the interesting way, but HOW do I call the add function?
    To be able to use it, I already need an _object_ and I dont have an object (which is
    definetly my only porblem) How can I can create an object of the e.g. MSFlexGrid
    ocx control???

    dim o as object
    set o= MSFlexGrid.add

    this fails (for sure)
    Any more hint?
    Uwe Reimann


  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Dynamically create controls/components at runtime

    the Add method works like this
    Controls.Add "classname of your control"
    e.g.

    Licenses.Add "MSFlexgridlib.msflexgrid"
    me.Controls.Add "MSFlexgridlib.msflexgrid", "h1"




    IMHO the load method is better suited for your needs


  5. #5
    Join Date
    May 1999
    Posts
    9

    Re: Dynamically create controls/components at runtime

    Ahh, finally what I really was looking for. Thanx alot!

    Since I realize there has been a similar post some time ago (which doesnt really answer this
    question), let me just summerize the results (how I understood them) for those who have
    a similar problem:

    If you want to dynamically create any control in VB6 you can use the "Add" function of
    the form's control collection.


    me.Controls.Add "MSFlexgridlib.msflexgrid", "h1"



    - Where "Me" is the object shortcut for the form whose code is currently evaluated.
    - "h1" is simply the name/key (you can name it as you like) how to find this control later
    within the collection, which could be done with:

    dim o as object
    set o=me.Controls.Item("h1")




    The Add routine will also return the object, so you dont have to add it and retrieve it
    in two steps, you can simply do:

    dim o as object
    set o=me.controls.add("MSFlexgridlib.msflexgrid", "h1")




    So back to my problem, where I want to get the control in one of the SSTab control
    tab pages.
    The SSTab control WONT offer a controls collection, so I CANT do the following:

    dim o as object
    set o=SSTABCTRL.controls.add("MSFlexgridlib.msflexgrid", "h1")



    (assuming SSTABCTRL is the name of the control I specified in the form editor)

    Anyway the SSTab (like most other controls, or even all?) offers a container property
    which specifies some kind of parent control/form/window in whose client area to
    draw itself.

    Finally this is the code that works for me:

    dim o as object
    set o=me.controls.add("MSFlexgridlib.msflexgrid", "h1")
    set o.container=SSTABCTRL
    o.visible=true




    One more word about the

    License.add "MSFlexgridlib.msflexgrid"



    If you have used the same control you want to dynamically create somewhere else
    on another form within the project, you dont need to add the license since this is
    already done by the project. Further you shouldnt do that, since it leads to a runtime
    error.
    But it's getting worse:
    If you add the control to your project by using the component manager but dont
    reference it somewhere on the forms at design time, the "add" function will throw
    an error since the default project setting says: "remove information for unused activex
    controls" (what is not too bad in general, but unhandy in this situation)
    The error message recommends to uncheck that project option (project->properties...
    ->Make->checkbox "remove information for unused activexcontrols" )
    This is IMHO only to recommend if you really know, that you have activated as few
    controls as you really need. You will gather a bunch of unneeded code if you select
    all possible controls in the component manager (to be able to use them in the form
    editor) and uncheck this checkbox. IMHO this would lead to the fact that all handler
    function code for every control will be added to you project. I cant estimate how
    much bigger or slower your executable will get, but it will get bigger (and bigger means
    slower).

    There still is a minor general question for solving similar problems in the future:
    The string "MSFlexgridlib.msflexgrid" used at the "add" functions, where do I find it?
    When adding the component to the project, there is nothing written about that class string.
    Further the VB-help of the control wont say anything about the "MSFlexgridlib". Is there
    a name convention, something like "CONTROLNAMElib" is the class?

    Sincerly yours,
    Uwe Reimann


  6. #6
    Join Date
    May 1999
    Posts
    3,332

    Re: Dynamically create controls/components at runtime

    >There still is a minor general question for solving similar problems in the future:
    >The string "MSFlexgridlib.msflexgrid" used at the "add" functions, where do I find it?

    you can add the component to the project and then hit F2 (object browser). you will then find the library (in the combobox) and the object in the object browser.




  7. #7
    Join Date
    Dec 2002
    Posts
    9

    Red face further question!

    Thanks! But how to handle the events of controls added by you. If we want to add many controls
    at runtime,how can I capture all events of every control?

  8. #8
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Code:
    ' Make sure there are no controls already on the form (ie user has gone backwards
    ' and forwards)
    ' I name my dynamic controls "dynamic" & ctl.type & key
    ' so a dynamic textbox would be called "dynamictextboxtxt1" for example.
    For Each ctl In frmResubmit.Controls
        If InStr(1, ctl.Name, "dynamic") Then
            frmResubmit.Controls.Remove ctl.Name
        End If
    Next ctl
    
    
    ' Next is a snippet of adding some controls dynamically
    ' Dynamically add the labels and text or date controls
    ' and get the widest label.
    For i = 0 To nAmount - 1
        Set lblObject = frmResubmit.Controls.ADD("VB.Label", "dynamiclbl" & i, frParams)
        With lblObject
            .Visible = True
            .AutoSize = True
            .Left = 200
            .Height = 375
            .Top = 300 + (.Height * i)
            .Caption = strName(i)
            If .Width + 300 > nWidth Then
                nWidth = .Width + 300
            End If
        End With
    
        If InStr(1, strName(i), "date") Then ' It is a date, add a Vencorp datepicker control
            Set dteObject = frmResubmit.Controls.ADD("NBDatePicker.VCDatePicker", "dynamictxt" & i, frParams)
    You will notice that you won't be able to find a "NBDatePicker.VCDatePicker" - this is because its one of my own controls where the NBDatePicker is the OCX, and the VCDatePicker is the name of the actual control.

    frParams is the control container - I believe its optional, but if you want your controls in a particular tab, you'll have to indicate which one it is.

    To handle the events of the controls, you'll have to do it manually
    Last edited by Cimperiali; December 3rd, 2003 at 12:44 AM.

  9. #9
    Join Date
    Mar 2002
    Location
    Izhevsk, Udmurtia, Russia
    Posts
    930
    Many thanks to you, Uwe Reimann
    There still is a minor general question for solving similar problems in the future:
    The string "MSFlexgridlib.msflexgrid" used at the "add" functions, where do I find it?
    When adding the component to the project, there is nothing written about that class string.
    Further the VB-help of the control wont say anything about the "MSFlexgridlib". Is there a name convention, something like "CONTROLNAMElib" is the class?
    The string "MSFlexgridlib.msflexgrid" is the PROGID of your control under which this control is registered in Windows Registry. There is no well-defined "name convention" for this.
    With best wishes,
    Vita
    -----------------------
    Russian Software Development Network -- http://www.rsdn.ru

  10. #10
    Join Date
    Nov 2003
    Posts
    32

    how to delete a control

    hi all,

    do anyone know how to delete a control at run time (for eg: a form created on command button click) that is created at runtime?

    thanks

  11. #11
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    You didn't look too hard at the post of mine above now, did you. The first paragraph shows how to delete.

    Code:
    ' Make sure there are no controls already on the form (ie user has gone backwards
    ' and forwards)
    ' I name my dynamic controls "dynamic" & ctl.type & key
    ' so a dynamic textbox would be called "dynamictextboxtxt1" for example.
    For Each ctl In frmResubmit.Controls
       If InStr(1, ctl.Name, "dynamic") Then
           frmResubmit.Controls.Remove ctl.Name
       End If
    Next ctl
    Be nice to Harley riders...

  12. #12
    Join Date
    Nov 2003
    Posts
    32
    i have to remove a form which i have created on run time

  13. #13
    Join Date
    Jul 2003
    Location
    Germany
    Posts
    39
    Remove:
    Set ctrlName = Nothing

    Or, more useful in runtime:
    Set Me.Controls("ProgID") = Nothing
    Yesterday it worked...

  14. #14
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    You have to remove a form that you've created at runtime....is not the same as what you originally asked, which was "how do I remove a CONTROL that was created at runtime"

    I think you need to call an api to 'destroy' the window - wizbang/cimperiali or one of the other api experts might be able to help a bit more - I don't play with api's too much.
    Be nice to Harley riders...

  15. #15
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Before invoking api...

    Let us see the code you used to create the form...
    What I mean is: if you did not use
    CreateWindow or CreateWindowEx, it should not be necessary
    to call the DestroyWindow Api...
    May be to unload it and set reference to nothing is enough...And if you add it to Forms, you should also be able to remove it



    By the way: I do not think I am an "api expert"
    Last edited by Cimperiali; December 2nd, 2003 at 08:27 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center