collection of class objects
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: collection of class objects

  1. #1
    Join Date
    Sep 2000
    Location
    Ottawa, Ontario
    Posts
    356

    collection of class objects

    Does anyone know if it's possible to create a collection of class objects withevents? If so is there a example of how to do this?

    Jean-Guy


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

    Re: collection of class objects

    It should be. Give me one day to check.

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...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.

  3. #3
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: collection of class objects

    It is not possible to have a collection that handles the events of the objects added to it....i.e. if you have a collection of command button objects, that collection will not be able to respond to one of them being clicked.


    HTH,
    D.

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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

    Re: collection of class objects

    It seems to me Francesco Balena (Vb Journal) did it with two classes to handle two collections, one without and one withevents. I may be wrong, but will let you know tomorrow.



    *********addendum*********
    http://www.**************/HtmlDoc.asp...&ID=310&Page=1
    Balena article; Take a look
    **************************
    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    Last edited by Cimperiali; August 26th, 2003 at 05:21 AM.
    ...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.

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

    Re: An example with commandbuttons

    Code:
    'This is a rearrangement of Francesco Balena Dynamic Forms Code
    'Of course, this is not as impressive as his example, but that is a lot
    'of code, so if you want it, mail me an E-mail where to send a zipped txt.
    [cimperiali: sorry, I lost the original file...]
    '-----------------------------------------------
    'one form
    'no controls on it
    '-----------------------------------------------
    'Two classes:
    'ControlItem
    'ControlItems
    '(code follows)
    '-----------------------------------------------
    'Form1 code
    option Explicit
    
    
    ' the collection of controls added dynamically
    Dim withevents ControlItems as ControlItems
    
    private Sub Form_Click()
    LoadControls
    End Sub
    
    private Sub Form_Resize()
        ' resize all controls to ensure that they fit in the form
        Dim ctrl as Control
        Dim ctrlItem as ControlItem
        
        on error resume next
        
        for Each ctrlItem In ControlItems
            set ctrl = ctrlItem.Control
            ctrl.Width = ctrlItem.Properties("Width")
            If ctrl.Left + ctrl.Width &gt; ScaleWidth then
                ctrl.Width = ScaleWidth - ctrl.Left
            End If
        next
    End Sub
    
    Sub LoadControls()
        'Dim index as Long
        Dim intX as Integer
        Dim ctrl as Control
        Dim ctrlItem as ControlItem
        Dim ctrlType as string
        Dim Properties as Collection
        Dim CustomProperties as Collection
        Dim top as Single
        Dim propItem as Variant
        Dim items() as string
        
        
        ' first, remove all controls added dynamically
        on error resume next
        ' don't try a forward or a for Each loop
        for intX = Controls.Count - 1 to 0 step -1
            Controls.Remove intX
        next
        on error GoTo 0
        
        ' start with a fresh ControlItems collection
        set ControlItems = new ControlItems
        
        ' initial value for Top property
        top = 100
        
        ' add controls corresponding to fields
        ' this demo program only supports a few field types
        for intX = 0 to 10
            ctrlType = ""
            set Properties = new Collection
            set CustomProperties = new Collection
              ctrlType = "Vb.Commandbutton"
                    Properties.Add "Caption=" & "Command" & intX
                
                ' now create the control
                set ctrl = Controls.Add(ctrlType, "CommandButton" & intX)
                ctrl.Move 1900, top, 2000, 315
                
                           
                
                ' set its other properties
                for Each propItem In Properties
                    ' split property name and value
                    items() = Split(propItem, "=")
                    CallByName ctrl, items(0), VbLet, items(1)
                next
                
                ctrl.Visible = true
                
                ' add this control to the ControlItems collection
                ' this will enable to receive events from it
                set ctrlItem = ControlItems.Add(ctrl)
                
                ' move the actual width into the Custom Width property
                ' this is used in the Form_Resize event
                ctrlItem.Properties.Add ctrl.Width, "Width"
                
                ' set its other custom properties
                for Each propItem In CustomProperties
                    ' split property name and value
                    items() = Split(propItem, "=")
                    ctrlItem.Properties.Add items(1), items(0)
                next
                
                ' increment top
                top = top + ctrl.Height + 80
       
        next
        
        ' force a Form_Resize event, to resize longer controls
        Call Form_Resize
        
        
        
    End Sub
    
    ' one control added dynamically is asking for validation
    ' Item.Control is a reference to the control
    ' Item.GetProperty(propname) returns a custom property
    
    private Sub ControlItems_Click(Item as ControlItem)
        MsgBox "You clicked on " & Item.Control.Caption
    End Sub
    
    
    
    '-------------------------------------------------------------
    
    'ControlItems Class code
    
    option Explicit
    
    ' this class raises an event in the parent form
    Event Click(Item as ControlItem)
    
    ' The private collection used to hold the real data
    private m_ControlItems as new Collection
    
    ' Add a new ControlItem item to the collection
    
    public Function Add(ctrl as Control) as ControlItem
        Dim newItem as new ControlItem
        newItem.Init ctrl, me
        
        ' add to the private collection
        m_ControlItems.Add newItem
        ' return the new item to the caller
        set Add = newItem
    End Function
    
    ' Remove an item from the collection
    
    public Sub Remove(index as Variant)
        m_ControlItems.Remove index
    End Sub
    
    ' Return a ControlItem item from the collection
    
    Function Item(index as Variant) as ControlItem
        set Item = m_ControlItems.Item(index)
    End Function
    
    ' Return the number of items in the collection
    
    property get Count() as Long
        Count = m_ControlItems.Count
    End property
    
    ' Remove all items from the collection
    
    public Sub Clear()
        set m_ControlItems = new Collection
    End Sub
    
    ' Implement support for enumeration (for Each)
    
    Function NewEnum() as IUnknown
        ' delegate to the private collection
        set NewEnum = m_ControlItems.[_NewEnum]
    End Function
    
    '---------------------------------------------
    ' friend Subs called when a ControlItem
    ' receives an event from Visual Basic
    '---------------------------------------------
    
    friend Sub Notify_Click(Item as ControlItem)
        ' raise a Validate event in the parent form
        RaiseEvent Click(Item)
    End Sub
    
    
    '-------------------------------------------------------------
    
    
    'ControlItem class Code:
    
    option Explicit
    
    ' a public collection for additional properties
    public Properties as new Collection
    ' the object being monitored - this is same as "Control"
    ' but it also receives events
    public withevents Control as CommandButton
    
    ' the parent ControlItems object
    Dim m_Parent as ControlItems
    
    ' initialize properties
    
    Sub Init(ctrl as CommandButton, parent as ControlItems)
        set Control = ctrl
        set m_Parent = parent
    End Sub
    
    ' retrieve a single property
    
    Function GetProperty(PropName as string, optional DefaultValue as Variant) as Variant
        on error resume next
        GetProperty = Properties(PropName)
        If Err And Not IsMissing(DefaultValue) then
            GetProperty = DefaultValue
        End If
    End Function
    
    ' the control has raised a Validate event
    
    private Sub Control_Click()
        ' notify it to the parent ControlItems class
        m_Parent.Notify_Click me
    End Sub


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    Last edited by Cimperiali; October 5th, 2003 at 04:23 AM.
    ...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.

  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: An example with commandbuttons

    If this works it will solve my problem of having a called classes events being reflected back to the creator class
    EG.
    '
    Class1 creates many copies of Class 2 which has a form. The clicks on the form are passed back to Class1. Is this what this sample is doing?

    John G

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

    Re: An example with commandbuttons

    Balena atricle (and code)
    http://www.**************/HtmlDoc.asp...&ID=310&Page=1
    '*****************************
    The project is a standard exe, with 1 form and two classes.
    The form istantiate class1 (ControlItems), which instantiates class2 (Controlitem). When clicking, and if I am not mistaking, class2 (Controlitem) raise event in class1(controlitems) which send it back to the form.
    By the way, thanks for rating (I would like to pass this 10 to Balena)
    and moreover: Balena did it in more complex form, which was a form able to dinamically add several types of field on the basis of fileds found opening a database of user's choice. He used windowless controls which are undocumented and in third disk of Visual studio ('Common-&gt;Tools-&gt;Vb-&gt;Winless copy the folder on your system, register ocx, make doubleclick on Mswless.reg)
    ...
    Cheers,
    Cesare

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    Last edited by Cimperiali; August 26th, 2003 at 05:22 AM.
    ...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.

  8. #8
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,055

    Re: An example with commandbuttons

    Cimperiali,

    Pls send me that file too, and thanks.

    Aio
    mximm@yahoo.com

    Marketing our skills - please participate in the survey and share your insights
    -

  9. #9
    Join Date
    Apr 1999
    Location
    Bangalore, Karnataka, India
    Posts
    72

    Re: An example with commandbuttons

    Hi,
    Can you please mail me that zipped txt which you mentioned.

    Prashant J.
    Prashant Joshi
    Itreya Technologies Pvt. Ltd.
    www.itreya.com

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

    Re: Zipped project sended

    Tell me if you do not receive them.
    Regards,

    Cesare Imperiali

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...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.

  11. #11
    Join Date
    Mar 2002
    Posts
    4

    Re: An example with commandbuttons

    I found some error in the sample,can you tell me why,thank you very much.
    It's in SUB "private Sub Form_Resize()","for Each ctrlItem In ControlItems"


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

    Re: Bug not found

    Sorry, I saw no bug, you should be more specific.
    The only thing is: for one of the two zipped project to work, you have to
    register the "MSWLESS.OCX" (= that is: the windowLess control library) which it
    is not installed by Vb setup, but that you can find on second or third disk of
    visual studio


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...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.

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

    Re: windowless controls library

    Correction:
    Windowless controls are undocumented and you can find them in third disk of Visual studio:
    Common-&gt;Tools-&gt;Vb-&gt;Winless
    Copy the folder on your system, register ocx, make doubleclick on Mswless.reg

    Regrads,

    Cesare Imperiali

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...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.

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