By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

Add Custom Properties To EventArgs Class

P: n/a
Hi all

I am creating a basic control to perform some tasks, and I want to declare
some events to be raised so they can be handled from the form that the
control is on. I can create my own Event Handler class and use that, but I
would like to use the System.EventArgs class so that my event can be handled
by different controls.

For example:

Private Sub WindowsButtonClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnButton.Click, myControl.myEventRaised
' Handle the event here
e.Cancel = True
End Sub

In the example, I would like to add 1 custom property called Cancel to the
System.EventArgs so that value is passed back to my control and I can
respond to it. Has anybody extended the System.EventArgs class to provide
custom functionality? A code snippett would be brilliant, but a link to an
easy to read resource would be much appreciated also.

Kind Regards,
Steve.
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Steve,
You can simply create your CustomEventArgs inherited from EventArgs class
I think this is possible in VB.NET

"Steve Amey" <stevea@_RemoveThis_centurion-ms.co.uk> wrote in message
news:eu*************@TK2MSFTNGP12.phx.gbl...
Hi all

I am creating a basic control to perform some tasks, and I want to declare
some events to be raised so they can be handled from the form that the
control is on. I can create my own Event Handler class and use that, but
I would like to use the System.EventArgs class so that my event can be
handled by different controls.

For example:

Private Sub WindowsButtonClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnButton.Click, myControl.myEventRaised
' Handle the event here
e.Cancel = True
End Sub

In the example, I would like to add 1 custom property called Cancel to the
System.EventArgs so that value is passed back to my control and I can
respond to it. Has anybody extended the System.EventArgs class to provide
custom functionality? A code snippett would be brilliant, but a link to an
easy to read resource would be much appreciated also.

Kind Regards,
Steve.

Jul 21 '05 #2

P: n/a
Hi Oleg

I know I can create my own class inherited from the EventArgs class, but how
do I add my custom property so that it is visible from outside the control?

The code I currently have:

' Control Class
Public Class MyControl

Public Event BeforeDelete(ByVal sender As Object, ByVal e As
System.EventArgs)

Private Sub DeleteRecord()
'// Raise the event to make sure that it is ok to delete the row
Dim oArgs As New BeforeDeleteEventArgs
RaiseEvent BeforeDelete(Me.Grid, oArgs)
If oArgs.Cancel.Equals(False) Then
' Delete the records
End If
End Sub

Public Class BeforeDeleteEventArgs
Inherits EventArgs
Private m_Cancel As Boolean
Public Property Cancel() As Boolean
Get
Return m_Cancel
End Get
Set(ByVal Value As Boolean)
m_Cancel = Value
End Set
End Property
End Class

End Class

In the form I want to do the following:

Private Sub MyControlBeforeDelete(ByVal sender As Object, ByVal e As
System.EventArgs) Handles myControl.BeforeDelete
e.Cancel = True ' I CANNOT see the Cancel property, how do I raise the
event so I see the properties
' of the BeforeDeleteEventArgs class.
End Sub

If I change the event so it uses the custom class I can do this:
Private Sub MyControlBeforeDelete(ByVal sender As Object, ByVal e As
BeforeDeleteEventArgs) Handles myControl.BeforeDelete
e.Cancel = True ' I CAN see the property if I do this, but I want to use
the System.EventArgs as the parameter
End Sub

In the form I want to use the System.EventArgs, not the custom class I have
created. How would I go about doing this?

Regards,
Steve.

"Oleg Medyanik" <me******@yahoo.com> wrote in message
news:OC**************@TK2MSFTNGP14.phx.gbl...
Steve,
You can simply create your CustomEventArgs inherited from EventArgs class
I think this is possible in VB.NET

"Steve Amey" <stevea@_RemoveThis_centurion-ms.co.uk> wrote in message
news:eu*************@TK2MSFTNGP12.phx.gbl...
Hi all

I am creating a basic control to perform some tasks, and I want to
declare some events to be raised so they can be handled from the form
that the control is on. I can create my own Event Handler class and use
that, but I would like to use the System.EventArgs class so that my event
can be handled by different controls.

For example:

Private Sub WindowsButtonClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnButton.Click, myControl.myEventRaised
' Handle the event here
e.Cancel = True
End Sub

In the example, I would like to add 1 custom property called Cancel to
the System.EventArgs so that value is passed back to my control and I can
respond to it. Has anybody extended the System.EventArgs class to provide
custom functionality? A code snippett would be brilliant, but a link to
an easy to read resource would be much appreciated also.

Kind Regards,
Steve.


Jul 21 '05 #3

P: n/a
Steve,
| Public Class BeforeDeleteEventArgs
| Inherits EventArgs
| Private m_Cancel As Boolean
| Public Property Cancel() As Boolean

| Public Class MyControl
|
| Public Event BeforeDelete(ByVal sender As Object, ByVal e As
| System.EventArgs)

Rather then define my own EventArgs & Delegate to handle canceling an Event,
I would recommend using System.ComponentModel.CancelEventArgs &
System.ComponentModel.CancelEventHandler.

http://msdn.microsoft.com/library/de...ClassTopic.asp

http://msdn.microsoft.com/library/de...ClassTopic.asp

Something like:

| Public Class MyControl
|
| Public Event BeforeDelete As System.ComponentModel.CancelEventHandler

Then I would be consistent with other events that are able to be canceled.
Such as the Form.Closing event.

http://msdn.microsoft.com/library/de...osingTopic.asp

I would not attempt to be "clever" and pass a CancelEventArg object for an
EventArg parameter, as any user (other developer) of your event would not
realize they needed to downcast the EventArg parameter to a CancelEventArg
to use it.

Hope this helps
Jay

"Steve Amey" <stevea@_RemoveThis_centurion-ms.co.uk> wrote in message
news:Oh**************@TK2MSFTNGP14.phx.gbl...
| Hi Oleg
|
| I know I can create my own class inherited from the EventArgs class, but
how
| do I add my custom property so that it is visible from outside the
control?
|
| The code I currently have:
|
| ' Control Class
| Public Class MyControl
|
| Public Event BeforeDelete(ByVal sender As Object, ByVal e As
| System.EventArgs)
|
| Private Sub DeleteRecord()
| '// Raise the event to make sure that it is ok to delete the row
| Dim oArgs As New BeforeDeleteEventArgs
| RaiseEvent BeforeDelete(Me.Grid, oArgs)
| If oArgs.Cancel.Equals(False) Then
| ' Delete the records
| End If
| End Sub
|
| Public Class BeforeDeleteEventArgs
| Inherits EventArgs
| Private m_Cancel As Boolean
| Public Property Cancel() As Boolean
| Get
| Return m_Cancel
| End Get
| Set(ByVal Value As Boolean)
| m_Cancel = Value
| End Set
| End Property
| End Class
|
| End Class
|
| In the form I want to do the following:
|
| Private Sub MyControlBeforeDelete(ByVal sender As Object, ByVal e As
| System.EventArgs) Handles myControl.BeforeDelete
| e.Cancel = True ' I CANNOT see the Cancel property, how do I raise the
| event so I see the properties
| ' of the BeforeDeleteEventArgs class.
| End Sub
|
| If I change the event so it uses the custom class I can do this:
| Private Sub MyControlBeforeDelete(ByVal sender As Object, ByVal e As
| BeforeDeleteEventArgs) Handles myControl.BeforeDelete
| e.Cancel = True ' I CAN see the property if I do this, but I want to use
| the System.EventArgs as the parameter
| End Sub
|
| In the form I want to use the System.EventArgs, not the custom class I
have
| created. How would I go about doing this?
|
| Regards,
| Steve.
|
| "Oleg Medyanik" <me******@yahoo.com> wrote in message
| news:OC**************@TK2MSFTNGP14.phx.gbl...
| > Steve,
| > You can simply create your CustomEventArgs inherited from EventArgs
class
| > I think this is possible in VB.NET
| >
| > "Steve Amey" <stevea@_RemoveThis_centurion-ms.co.uk> wrote in message
| > news:eu*************@TK2MSFTNGP12.phx.gbl...
| >> Hi all
| >>
| >> I am creating a basic control to perform some tasks, and I want to
| >> declare some events to be raised so they can be handled from the form
| >> that the control is on. I can create my own Event Handler class and
use
| >> that, but I would like to use the System.EventArgs class so that my
event
| >> can be handled by different controls.
| >>
| >> For example:
| >>
| >> Private Sub WindowsButtonClick(ByVal sender As Object, ByVal e As
| >> System.EventArgs) Handles btnButton.Click, myControl.myEventRaised
| >> ' Handle the event here
| >> e.Cancel = True
| >> End Sub
| >>
| >> In the example, I would like to add 1 custom property called Cancel to
| >> the System.EventArgs so that value is passed back to my control and I
can
| >> respond to it. Has anybody extended the System.EventArgs class to
provide
| >> custom functionality? A code snippett would be brilliant, but a link to
| >> an easy to read resource would be much appreciated also.
| >>
| >> Kind Regards,
| >> Steve.
| >>
| >
| >
|
|
Jul 21 '05 #4

P: n/a
Hi Jay

Thank you for your advice, I have changed the event to use the
CancelEventArgs instead. I'm glad I used that example because you told me a
better way to deal with the scenario, but there are other times that I need
to define more custom properties like DataRow's or DataTable's. Do you have
any links to good topics on extending the EventArgs class when the
CancelEventArgs class won't provide the required functionality?

It's not a really big deal, but I would like to know how to accomplish it to
save me having 2 functions that handle 2 events when I could probably have
just the 1.

Thank you for your help.

Kind Regards,
Steve

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
Steve,
| Public Class BeforeDeleteEventArgs
| Inherits EventArgs
| Private m_Cancel As Boolean
| Public Property Cancel() As Boolean

| Public Class MyControl
|
| Public Event BeforeDelete(ByVal sender As Object, ByVal e As
| System.EventArgs)

Rather then define my own EventArgs & Delegate to handle canceling an
Event,
I would recommend using System.ComponentModel.CancelEventArgs &
System.ComponentModel.CancelEventHandler.

http://msdn.microsoft.com/library/de...ClassTopic.asp

http://msdn.microsoft.com/library/de...ClassTopic.asp

Something like:

| Public Class MyControl
|
| Public Event BeforeDelete As System.ComponentModel.CancelEventHandler

Then I would be consistent with other events that are able to be canceled.
Such as the Form.Closing event.

http://msdn.microsoft.com/library/de...osingTopic.asp

I would not attempt to be "clever" and pass a CancelEventArg object for an
EventArg parameter, as any user (other developer) of your event would not
realize they needed to downcast the EventArg parameter to a CancelEventArg
to use it.

Hope this helps
Jay

"Steve Amey" <stevea@_RemoveThis_centurion-ms.co.uk> wrote in message
news:Oh**************@TK2MSFTNGP14.phx.gbl...
| Hi Oleg
|
| I know I can create my own class inherited from the EventArgs class, but
how
| do I add my custom property so that it is visible from outside the
control?
|
| The code I currently have:
|
| ' Control Class
| Public Class MyControl
|
| Public Event BeforeDelete(ByVal sender As Object, ByVal e As
| System.EventArgs)
|
| Private Sub DeleteRecord()
| '// Raise the event to make sure that it is ok to delete the row
| Dim oArgs As New BeforeDeleteEventArgs
| RaiseEvent BeforeDelete(Me.Grid, oArgs)
| If oArgs.Cancel.Equals(False) Then
| ' Delete the records
| End If
| End Sub
|
| Public Class BeforeDeleteEventArgs
| Inherits EventArgs
| Private m_Cancel As Boolean
| Public Property Cancel() As Boolean
| Get
| Return m_Cancel
| End Get
| Set(ByVal Value As Boolean)
| m_Cancel = Value
| End Set
| End Property
| End Class
|
| End Class
|
| In the form I want to do the following:
|
| Private Sub MyControlBeforeDelete(ByVal sender As Object, ByVal e As
| System.EventArgs) Handles myControl.BeforeDelete
| e.Cancel = True ' I CANNOT see the Cancel property, how do I raise the
| event so I see the properties
| ' of the BeforeDeleteEventArgs class.
| End Sub
|
| If I change the event so it uses the custom class I can do this:
| Private Sub MyControlBeforeDelete(ByVal sender As Object, ByVal e As
| BeforeDeleteEventArgs) Handles myControl.BeforeDelete
| e.Cancel = True ' I CAN see the property if I do this, but I want to
use
| the System.EventArgs as the parameter
| End Sub
|
| In the form I want to use the System.EventArgs, not the custom class I
have
| created. How would I go about doing this?
|
| Regards,
| Steve.
|
| "Oleg Medyanik" <me******@yahoo.com> wrote in message
| news:OC**************@TK2MSFTNGP14.phx.gbl...
| > Steve,
| > You can simply create your CustomEventArgs inherited from EventArgs
class
| > I think this is possible in VB.NET
| >
| > "Steve Amey" <stevea@_RemoveThis_centurion-ms.co.uk> wrote in message
| > news:eu*************@TK2MSFTNGP12.phx.gbl...
| >> Hi all
| >>
| >> I am creating a basic control to perform some tasks, and I want to
| >> declare some events to be raised so they can be handled from the form
| >> that the control is on. I can create my own Event Handler class and
use
| >> that, but I would like to use the System.EventArgs class so that my
event
| >> can be handled by different controls.
| >>
| >> For example:
| >>
| >> Private Sub WindowsButtonClick(ByVal sender As Object, ByVal e As
| >> System.EventArgs) Handles btnButton.Click, myControl.myEventRaised
| >> ' Handle the event here
| >> e.Cancel = True
| >> End Sub
| >>
| >> In the example, I would like to add 1 custom property called Cancel
to
| >> the System.EventArgs so that value is passed back to my control and I
can
| >> respond to it. Has anybody extended the System.EventArgs class to
provide
| >> custom functionality? A code snippett would be brilliant, but a link
to
| >> an easy to read resource would be much appreciated also.
| >>
| >> Kind Regards,
| >> Steve.
| >>
| >
| >
|
|

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.