473,385 Members | 1,356 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.

How to invoke a custom event in a derived control?

Hello fellow developers,

I've created a control which derives from the .NET TextBox. I then
wanted to add two events to the new control, so I added the following
to the control code:

public event EventHandler InvalidValueMatched;
public event EventHandler ValidValueMatched;

But when one of the events is raised I get this exception:

System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="UserResponseControl"
StackTrace:
at
CustomControls.ValidationTextBox.OnValidating(Canc elEventArgs e)
at
System.Windows.Forms.Control.PerformControlValidat ion(Boolean
bulkValidation)
at
System.Windows.Forms.ContainerControl.ValidateThro ughAncestor(Control
ancestorControl, Boolean preventFocusChangeOnError)

By the way, I raise an event with the following code:
InvalidValueMatched.Invoke(this, new EventArgs());

How can this problem be solved? I have googled a lot, but none of the
tutorials/samples I can find, matches my scenario. Since I'm also
pretty new to custom controls, I'd like to know if I'm doing anything
wrong (ex. can I just pass a new EventArgs()?).

Thanks in advance!

- Dan

Feb 28 '07 #1
4 2749
Hello fellow developers,
>
I've created a control which derives from the .NET TextBox. I then
wanted to add two events to the new control, so I added the following
to the control code:

public event EventHandler InvalidValueMatched;
public event EventHandler ValidValueMatched;

But when one of the events is raised I get this exception:

System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="UserResponseControl"
StackTrace:
at
CustomControls.ValidationTextBox.OnValidating(Canc elEventArgs e)
at
System.Windows.Forms.Control.PerformControlValidat ion(Boolean
bulkValidation)
at
System.Windows.Forms.ContainerControl.ValidateThro ughAncestor(Control
ancestorControl, Boolean preventFocusChangeOnError)

By the way, I raise an event with the following code:
InvalidValueMatched.Invoke(this, new EventArgs());

How can this problem be solved? I have googled a lot, but none of the
tutorials/samples I can find, matches my scenario. Since I'm also
pretty new to custom controls, I'd like to know if I'm doing anything
wrong (ex. can I just pass a new EventArgs()?).

Thanks in advance!
It looks like no client has subscribed to your event. Fixing that will
likely fix the problem but you need to check "InvalidValueMatched" for null
before invoking it anyway. Moreover, it should be called inisde a try/catch
block since the event client (sink object) might throw an exception which
you'll want to catch and often (usually) ignore. The situation actually runs
deeper however since you'll probably want to continue firing your event to
any remaining clients. That is, just because one client throws an exception
doesn't mean you should stop firing to any remaining clients. I suggest
getting hold of a copy of "Programming .NET Components" by Juval Lowy. A
recognized .NET expert by MSFT (which becomes clear once you start reading
it), he goes into great detail on the subject, covering everything you'll
ever want to know. It's well worth the money even for experienced
developers.
Feb 28 '07 #2
On Feb 28, 9:34 pm, "Larry Smith" <no_spam@_nospam.comwrote:
Hello fellow developers,
I've created a control which derives from the .NET TextBox. I then
wanted to add two events to the new control, so I added the following
to the control code:
public event EventHandler InvalidValueMatched;
public event EventHandler ValidValueMatched;
But when one of the events is raised I get this exception:
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="UserResponseControl"
StackTrace:
at
CustomControls.ValidationTextBox.OnValidating(Canc elEventArgs e)
at
System.Windows.Forms.Control.PerformControlValidat ion(Boolean
bulkValidation)
at
System.Windows.Forms.ContainerControl.ValidateThro ughAncestor(Control
ancestorControl, Boolean preventFocusChangeOnError)
By the way, I raise an event with the following code:
InvalidValueMatched.Invoke(this, new EventArgs());
How can this problem be solved? I have googled a lot, but none of the
tutorials/samples I can find, matches my scenario. Since I'm also
pretty new to custom controls, I'd like to know if I'm doing anything
wrong (ex. can I just pass a new EventArgs()?).
Thanks in advance!

It looks like no client has subscribed to your event. Fixing that will
likely fix the problem but you need to check "InvalidValueMatched" for null
before invoking it anyway. Moreover, it should be called inisde a try/catch
block since the event client (sink object) might throw an exception which
you'll want to catch and often (usually) ignore. The situation actually runs
deeper however since you'll probably want to continue firing your event to
any remaining clients. That is, just because one client throws an exception
doesn't mean you should stop firing to any remaining clients. I suggest
getting hold of a copy of "Programming .NET Components" by Juval Lowy. A
recognized .NET expert by MSFT (which becomes clear once you start reading
it), he goes into great detail on the subject, covering everything you'll
ever want to know. It's well worth the money even for experienced
developers.
Great, it works now! I'll have a look at the book as soon as possible.
I just have one more question: you suggest that I should invoke the
event inside a try/catch block, I can completely understand this. But,
isn't it bad practice to "ignore" an exception? Should I just leave
the catch-block empty and forget about the exception?

Thanks again!

- Dan

Feb 28 '07 #3
Great, it works now! I'll have a look at the book as soon as possible.
I just have one more question: you suggest that I should invoke the
event inside a try/catch block, I can completely understand this. But,
isn't it bad practice to "ignore" an exception? Should I just leave
the catch-block empty and forget about the exception?
Only you know for sure in your particular app but generally speaking you
should strive to ignore it if possible. The reason is straight-forward
enough. The (source) object firing the event usually isn't tightly coupled
with its clients so it normally doesn't know who the clients (sink objects)
of the event are. The source object just fires the event to all interested
parties and that's all. If a particular sink object throws an exception, the
source object might not know how to deal with the problem. It may not know
who the clients even are (especially if its a generic object dealing with
external clients) nor what exceptions they might throw nor how to handle
them (which will vary from client-to-client). Imagine if your own app
registered for an event with a DB server for instance (one serving other
potential clients). What should that server do if you throw an exception. It
should catch it for sure because it shouldn't crash. It should also continue
firing the event to other clients. It really can't deal with your exception
however except gracefully back out of what it's doing and perhaps log it as
a courtesy for you. It shouldn't really be responsible for your program's
errors however so exceptions shouldn't normally be propagated back in the
first place. You should usually prevent it that is. Within your own app of
course you can always do whatever's appropriate however. The source object
might want to log the error, abort the app or whatever but other clients may
be waiting for the event to be fired so it might perilous to ignore them
(or, depending on your app, maybe they'll now have a problem given that the
other client failed). The looser the coupling is between source and sink
objects, the better your program will be.
Feb 28 '07 #4
On Feb 28, 11:00 pm, "Larry Smith" <no_spam@_nospam.comwrote:
Great, it works now! I'll have a look at the book as soon as possible.
I just have one more question: you suggest that I should invoke the
event inside a try/catch block, I can completely understand this. But,
isn't it bad practice to "ignore" an exception? Should I just leave
the catch-block empty and forget about the exception?

Only you know for sure in your particular app but generally speaking you
should strive to ignore it if possible. The reason is straight-forward
enough. The (source) object firing the event usually isn't tightly coupled
with its clients so it normally doesn't know who the clients (sink objects)
of the event are. The source object just fires the event to all interested
parties and that's all. If a particular sink object throws an exception, the
source object might not know how to deal with the problem. It may not know
who the clients even are (especially if its a generic object dealing with
external clients) nor what exceptions they might throw nor how to handle
them (which will vary from client-to-client). Imagine if your own app
registered for an event with a DB server for instance (one serving other
potential clients). What should that server do if you throw an exception. It
should catch it for sure because it shouldn't crash. It should also continue
firing the event to other clients. It really can't deal with your exception
however except gracefully back out of what it's doing and perhaps log it as
a courtesy for you. It shouldn't really be responsible for your program's
errors however so exceptions shouldn't normally be propagated back in the
first place. You should usually prevent it that is. Within your own app of
course you can always do whatever's appropriate however. The source object
might want to log the error, abort the app or whatever but other clients may
be waiting for the event to be fired so it might perilous to ignore them
(or, depending on your app, maybe they'll now have a problem given that the
other client failed). The looser the coupling is between source and sink
objects, the better your program will be.
I can see it now, great explanation! Thanks again!

- Dan

Mar 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David | last post by:
I have a custom control that contains a class that Inherits the panel control. I am trying to catch a keydown event in this class and then supress it from the rest of the control as well as the...
2
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method...
4
by: Paradox | last post by:
Hey, I'm trying to figure out what situations call for the use of a derived form control such as: public class myListBox : System.Windows.Forms.ListBox and what situations call for the use...
1
by: Dennis | last post by:
I'm using VS 2003 my project uses COM interop and the Tao Framework. I have a custom control derived from UserControl. It implements many functions similar to the Tao's SimpleOpenGLControl, for...
3
by: TIBM | last post by:
Hi. I'm not getting any answers in buildingcontrols, so I'm gonna try here. I've written a custom control derived from System.Web.UI.WebControls.ListControl. The control is taking in a dataset and...
5
by: | last post by:
I am wondering what the best method of attaching custom Events to custom WebUserControls are. I cannot seem to find the proper terminology to expand my research. Basicallly I have a custom user...
4
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is...
2
by: Michal Valent | last post by:
I would like to fire some custom server control event before Page_Load event like this (from the trace of an aspx page) : Trace Information Category Message From First(s) From Last(s) aspx.page...
4
by: =?Utf-8?B?UmljaEI=?= | last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I would like to dynamically add panes to the control with a form template added when the pane is added. I have tried...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.