Results 1 to 6 of 6

Thread: [RESOLVED] WithEvents Array

  1. #1

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Resolved [RESOLVED] WithEvents Array

    I have a Class where i manipulate with one Form. That Form is declared WithEvents:
    Code:
    Private WithEvents m_frmOwner As Form
    Now i would like to expand this and make it into an Array. But i don't know how. Something like:
    Code:
    Private WithEvents m_frmOwners() As Form
    ... doesn't work.

    Any ideas?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: WithEvents Array

    You might find this FAQ article useful: Why can't I use WithEvents on arrays of objects? (it gives an alternative)

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: WithEvents Array

    You could add the forms to a collection when you initialize them. Then reference it as a collection member as usual

    Something like this:
    vb Code:
    1. 'Declare
    2. Private WithEvents m_frmOwner As Form1
    3. Private FormClln as new Collection
    4. ....
    5. 'Initialize form and add to forms collection
    6. m_frmOwners = new Form1
    7. FormClln.Add(m_frmOwners)
    8. ...
    9. 'reference later like this
    10. fForm = FormClln(0)
    11. fForm.Caption = "whatever"

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: WithEvents Array

    The more or less obvious issue with Pradeep's suggestion is that only one form can be referenced to that single variable at a time. Thus one object from the collection would be have accessible events and the rest wouldn't as there wouldn't be a WithEvents reference.

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: WithEvents Array

    How many Forms raise events anyway?

  6. #6

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: WithEvents Array

    @si_the_geek: this actually solves my problem, but, i managed to solve it differently. With a new, superior Class to this, that simply includes an Array of first Class instances. This shortens my "Form code" and allows me to manage all first Class instances quite simply

    @Pradeep1210: i have looked at this solution before you even posted it. I think Merri wrapped the first and most important problem that comes along.

    @dilettante: not necessarily all. But, it can happen.

    Thank you all

    Resolved!

Posting Permissions

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



Click Here to Expand Forum to Full Width