Results 1 to 13 of 13

Thread: How do I use WithEvents?

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    How do I use WithEvents?

    I have seen WithEvents used before and I understand it somewhat but I am looking for some clarification. How far can its use be extended? Can I declare an object WithEvents without knowing what will be in it?

    I am working on making a custom control and I have an Init() method. In that method you declare which control on the form you want to link my control to. I know the control will always have a Resize event, but I don't always know what the event will be. Is there a way to use WithEvents to access the Resize event?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: How do I use WithEvents?

    Can anyone help me with this?

    I experimented a little and found that doing WithEvents with an Object doesn't work because you have to specify a specific control. Is there any way to get access to the events of an object without knowing what object it will be, but knowing it will have a Resize event?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I use WithEvents?

    The events are part of the class's interface, so you can't declare a generic Object WithEvents. What you can do, I think, is declare an instance interface class WithEvents and make all your classes Implement that interface. Pop your events in there and it should work (assuming VB6 handles interfaces the way I think).

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: How do I use WithEvents?

    You can create a generic event handler for ActiveX controls using the VBControlExtender object.

    VB Code:
    1. Private WithEvents oControlEvents As VBControlExtender
    2.  
    3. Private Sub Form_Load()
    4.     Set oControlEvents = Me.MSFlexGrid1
    5. End Sub
    6.  
    7. Private Sub oControlEvents_ObjectEvent(Info As EventInfo)
    8.     Dim oParam As EventParameter
    9.  
    10.     Debug.Print Info.Name;
    11.  
    12.     For Each oParam In Info.EventParameters
    13.         Debug.Print , oParam.Name, oParam.Value
    14.     Next
    15.  
    16. End Sub

    Note that VB intrinsic controls do not implement the ControlExtender.

  5. #5
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: How do I use WithEvents?

    Attached is a simple example of using Withevents from your own classes.

    Hope this helps!!
    Attached Files Attached Files

  6. #6

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: How do I use WithEvents?

    Quote Originally Posted by penagate
    The events are part of the class's interface, so you can't declare a generic Object WithEvents. What you can do, I think, is declare an instance interface class WithEvents and make all your classes Implement that interface. Pop your events in there and it should work (assuming VB6 handles interfaces the way I think).
    What is an instance interface class?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: How do I use WithEvents?

    Quote Originally Posted by brucevde
    You can create a generic event handler for ActiveX controls using the VBControlExtender object.

    Note that VB intrinsic controls do not implement the ControlExtender.
    By intrinsic control, do you mean the standard ones that are always available?

    If thats the case then I am stuck again. Basically I need to be able to get the events of a PictureBox OR a custom control.

    I couldn't even subclass if I wanted to because it is likely that the controls I will be dealing with don't have .hWnds.

    Do I have any other options?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  8. #8
    Lively Member rm_03's Avatar
    Join Date
    Aug 2004
    Posts
    92

    Re: How do I use WithEvents?

    Quote Originally Posted by penagate
    The events are part of the class's interface, so you can't declare a generic Object WithEvents. What you can do, I think, is declare an instance interface class WithEvents and make all your classes Implement that interface. Pop your events in there and it should work (assuming VB6 handles interfaces the way I think).
    As far as I understood the whole event stuff, interfaces which support events
    only implement IConnectionPointContainer, but the events have their own interface.
    So what you do if you want events:
    get IConnectionPointContainer from the interface,
    take the first IConnectionPoint returned by IConnectionPointContainer.EnumConnectionPoints,
    create a new interface which implements IDispatch (event sink),
    advise your event sink to IConnectionPoint,
    and capture events through IDispatch.Invoke, which the interface will call everytime an event get's raised.

  9. #9

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: How do I use WithEvents?

    Could I get you to post a sample project? Because I have no idea what you just said.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I use WithEvents?

    Quote Originally Posted by rm_03
    get IConnectionPointContainer from the interface
    How do you do this? Is it just a matter of casting the object?

  11. #11
    Lively Member rm_03's Avatar
    Join Date
    Aug 2004
    Posts
    92

    Re: How do I use WithEvents?

    Quote Originally Posted by penagate
    How do you do this? Is it just a matter of casting the object?
    Yes.
    Like:
    VB Code:
    1. Set IConnectionPointContainer = object
    Or:
    VB Code:
    1. IUnknown.QueryInterface IID_IConnectionPointContainer, IConnectionPointContainer

    Quote Originally Posted by eyeRmonkey
    Could I get you to post a sample project? Because I have no idea what you just said.
    Sure
    But it's rather complicated, I didn't want to use a type library.
    Attached Files Attached Files

  12. #12

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: How do I use WithEvents?

    What type library would make it easier?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  13. #13
    Lively Member rm_03's Avatar
    Join Date
    Aug 2004
    Posts
    92

    Re: How do I use WithEvents?

    One which defines the interfaces used in the example (IUnk/IDisp/ICPC/ICP).
    I think it would shorten the code about 40-50%. I'm just too lazy

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