Add Custom Properties To EventArgs Class

  • Thread starter Thread starter Steve Amey
  • Start date Start date
S

Steve Amey

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.
 
Steve,
You can simply create your CustomEventArgs inherited from EventArgs class
I think this is possible in VB.NET
 
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.
 
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/d...emComponentModelCancelEventArgsClassTopic.asp

http://msdn.microsoft.com/library/d...omponentModelCancelEventHandlerClassTopic.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/d...rfSystemWindowsFormsFormClassClosingTopic.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

| 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.
|
| | > Steve,
| > You can simply create your CustomEventArgs inherited from EventArgs
class
| > I think this is possible in VB.NET
| >
| > | >> 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.
| >>
| >
| >
|
|
 
Back
Top