473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Add Custom Properties To EventArgs Class

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
4 4724
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
2
by: Technical Support | last post by:
Good afternoon, I created a custom control, compiled it into a DLL, and added it to the BIN directory. I placed an instance of the control in a webForm and am trying to programatically change its...
2
by: fred | last post by:
I am trying to Copy and paste using a custom format but I canot retrieve the data. In the main Form's class I have declared: Dim myDocmanFormat as DataFormats.Format then in the New Sub of...
8
by: Steve Amey | last post by:
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...
3
by: | last post by:
Hi all, I have a question on reflection Lets say I have a custom object called Address. Now, lets say I have a string variable that holds the name of a variable in the object such as...
0
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object ...
4
by: Suresh | last post by:
Is there any way to access the custom properties of a master page from the aspx form? I know the custom properties of a master page can be accessed from the aspx.cs partial class by specifying...
11
by: Pete Kane | last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would contain tab pages of my own classes, I know you can achieve this with...
2
by: trialproduct2004 | last post by:
Hi all, I am having application in whihc i am inserting menuitem dynamically. When i add menuitem to mainmenu, i want to pass extra parameter to eventargs. So what i did is derived class from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.