473,383 Members | 1,958 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,383 software developers and data experts.

Testing for the existance of an Event Handler

Is there a way to test to see if an event handler hasn't already been
added to an event handler listener object? This is what I am doing now
as a hack:

dataView.ListChanged -= new
ListChangedEventHandler(dataView_ListChanged);
dataView.ListChanged += new
ListChangedEventHandler(dataView_ListChanged);
Instead, is there something like:

if (!dataView.ListChanged.Exists("dataView_ListChange d"))
{
dataView.ListChanged += new
ListChangedEventHandler(dataView_ListChanged);
}

thanks,
dave
Nov 16 '05 #1
1 4991
"malcolm" <ch********@yahoo.com> wrote in message news:4f**************************@posting.google.c om...
Is there a way to test to see if an event handler hasn't already been
added to an event handler listener object?
public bool IsHandlerAttached( MyEventHandler eh)
{
return ((IList)(MyEvent.GetInvocationList( ))).Contains( eh);
}

However, this functionality must be performed within the class that
exposes MyEvent. Outside of the class, you can't access MyEvent
as if it were a Delegate (which is what it is).
This is what I am doing now as a hack:

dataView.ListChanged -= new
ListChangedEventHandler(dataView_ListChanged);
dataView.ListChanged += new
ListChangedEventHandler(dataView_ListChanged);


Why not just save the EventHandler in a field? Like this,

private ListChangedEventHandler m_notifyListChanged = null;
// . . .
// Attach an event handler.
this.m_notifyListChanged = new ListChangedEventHandler( dataView_ListChanged);
dataView.ListChanged += this.m_notifyListChanged;
// . . .
// Ask yourself if you've attached an event handler.
if ( null != this.m_notifyListChanged )
{
// If so, detach the event handler.
//
dataView.ListChanged -= this.m_notifyListChanged;
this.m_notifyListChanged = null;
}
// . . .
Derek Harmon
Nov 16 '05 #2

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

Similar topics

7
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. ...
7
by: Joakim Braun | last post by:
Why doesn't the below code work? I'm trying to create a global object and set an event handler to one of its methods. The function is called, but the object's mTest property is undefined. ...
2
by: Javaman59 | last post by:
I want to add a ValueChanged event handler to a control. If I add the handler during form construction then the ValueChanged handler gets called when the control is initialized, and I don't want...
4
by: Iain Porter | last post by:
Hi all, I have a usercontrol (a textbox with dropdown calendar that fills the textbox with the selected date) that I implement in a datagrid in a webform. On first loading the page, the datagrid's...
3
by: Beth | last post by:
in the following: this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click); if I saw an equation, such as y +=x; then y = y+x. But what is the meaning in the event handler. I...
2
by: ashish | last post by:
Hi all, Is is possible to add event handler for ProfileModule.MigrateAnonymous event outside global.asax ?, the thing is that i want to be able to write some unit tests and that would pretty...
0
by: Mayur Gadhave | last post by:
Hello all, Im trying to call my .net c# library functions from my unmanaged vc++(dialog based MFC) application . From that application i want add the event handler for the event generated in c#...
0
by: Mayur | last post by:
Hello all, Im trying to call my .net c# library functions from my unmanaged vc++(dialog based MFC) application . From that application i want add the event handler for the event generated in c#...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.