Home All Groups Group Topic Archive Search About
Author
11 Aug 2006 10:01 AM
Theo Verweij
Does anyone know if there is a way to check if an event is handled?

Example code:

If EventIsHandled then 'Is this possible?
   RaiseEvent MyEvent(Me, New System.EventArgs)
Else
   'Do something else ...
End If

Author
11 Aug 2006 6:14 PM
tomb
Theo Verweij wrote:

> Does anyone know if there is a way to check if an event is handled?
>
> Example code:
>
> If EventIsHandled then 'Is this possible?
>   RaiseEvent MyEvent(Me, New System.EventArgs)
> Else
>   'Do something else ...
> End If

An event handler is the result of having raised an event.  That's where
you put the code to execute, in the body of the event handler.
Why would you want to raise some other event if an event is raised?  And
if the event is not raised, won't your application just proceed as normal?

T
Author
11 Aug 2006 6:56 PM
Theo Verweij
Yes, I know how it works.

But what I try to do is give the user of the component the possibility
to take some actions, like formating or translating some strings by
implementing/handling an event.

When the user of this component chooses not to implement this, I want
the component to fall back to a default formatting.

An example:

If EventIsHandled(MyEvent) then 'just made up some syntax
    RaiseEvent MyEvent(Me, New MyFormattingEventArgs(aStringToFormat))
Else
    'Do the standard formatting
End If

And, by typing this, I found the solution.
I just have to put the value of Nothing into the FormattedResult of
MyFormattingEventArgs.
If it is still Nothing after RaiseEvent, the event was not handled, so I
can do the standard formatting.

Thanks for your input!


tomb wrote:
Show quoteHide quote
> Theo Verweij wrote:
>
>> Does anyone know if there is a way to check if an event is handled?
>>
>> Example code:
>>
>> If EventIsHandled then 'Is this possible?
>>   RaiseEvent MyEvent(Me, New System.EventArgs)
>> Else
>>   'Do something else ...
>> End If
>
> An event handler is the result of having raised an event.  That's where
> you put the code to execute, in the body of the event handler.
> Why would you want to raise some other event if an event is raised?  And
> if the event is not raised, won't your application just proceed as normal?
>
> T
Author
11 Aug 2006 11:29 PM
David Anton
You need to use the hidden (nearly undocumented) <event>Event variable:

If MyEventEvent Then
   RaiseEvent MyEvent(Me, New System.EventArgs)
Else
   'Do something else ...
End If

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#


Show quoteHide quote
"Theo Verweij" wrote:

> Does anyone know if there is a way to check if an event is handled?
>
> Example code:
>
> If EventIsHandled then 'Is this possible?
>    RaiseEvent MyEvent(Me, New System.EventArgs)
> Else
>    'Do something else ...
> End If
>
Author
11 Aug 2006 11:54 PM
Herfried K. Wagner [MVP]
"David Anton" <DavidAn***@discussions.microsoft.com> schrieb:
> You need to use the hidden (nearly undocumented) <event>Event variable:
>
> If MyEventEvent Then
>   RaiseEvent MyEvent(Me, New System.EventArgs)
> Else
>   'Do something else ...
> End If

ACK, but the test is not even necessary because it's already being performed
by 'RaiseEvent'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
12 Aug 2006 12:56 AM
David Anton
Personally, I don't do this, but I've come across a lot of code where the
hidden Event variable is checked to ensure that if it is null/Nothing, some
other branch of code is executed (even though the RaiseEvent checks for it
anyway).
Btw, my earlier post should have had:
If Not MyEventEvent Is Nothing Then
    RaiseEvent MyEvent(Me, New System.EventArgs)
Else
    'Do something else ...
End If
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#


Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "David Anton" <DavidAn***@discussions.microsoft.com> schrieb:
> > You need to use the hidden (nearly undocumented) <event>Event variable:
> >
> > If MyEventEvent Then
> >   RaiseEvent MyEvent(Me, New System.EventArgs)
> > Else
> >   'Do something else ...
> > End If
>
> ACK, but the test is not even necessary because it's already being performed
> by 'RaiseEvent'.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>
Author
12 Aug 2006 12:46 PM
Theo Verweij
Thank you very much!

David Anton wrote:
Show quoteHide quote
> Personally, I don't do this, but I've come across a lot of code where the
> hidden Event variable is checked to ensure that if it is null/Nothing, some
> other branch of code is executed (even though the RaiseEvent checks for it
> anyway).
> Btw, my earlier post should have had:
> If Not MyEventEvent Is Nothing Then
>     RaiseEvent MyEvent(Me, New System.EventArgs)
> Else
>     'Do something else ...
> End If