Re: How to Verify if EventHandler for ValueChanged has been set?

Tech-Archive recommends: Fix windows errors by optimizing your registry



If you still going to use reflection, you can use something like that:

private bool AreListenersAdded(TextBox control, string eventName)
{
var controlType = control.GetType();

var fields = controlType.GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
var eventField = fields.SingleOrDefault(f => f.Name == "Event" + eventName);
if (eventField != null)
{
var eventsProperty = (typeof(Control)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
var events = eventsProperty.GetValue(control, null) as EventHandlerList;
if (events != null)
{
var textChangedEvent = eventField.GetValue(control);
var eventDelegate = events[textChangedEvent];
if (eventDelegate != null)
return eventDelegate.GetInvocationList().Length > 0;
}

}


return false;
}

<richard.martino@xxxxxxxxxxxxxxxx> wrote in message news:6b9b3795-ad42-4460-b598-17582f09dbd2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Dec 16, 1:12 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Richard,

You would have to use reflection for this, as you don't have access to
the event handler list from outside the instance that exposes the event.

You would have to look for the backing field for the delegate, but the
thing is, there is no guaranteed linking between that field and the event
exposed. For the standard event implementation (no add/remove handlers), it
should work, but you might run into some custom add/remove handlers and it
won't be that easy to figure out what the backing field for the event is.

Are you doing this for unit testing, or within your app? If you are
doing it within your app, I would recommend making this a unit test or maybe
a custom rule for FxCop.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx

<richard.mart...@xxxxxxxxxxxxxxxx> wrote in message

news:6b0427f8-3b02-406a-af2f-b8145011866f@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



>I have a huge Windows desktop application with over 100 individual
> controls, like TextBox, NumericUpDown, ComboBox, etc.

> I want each one to have its ValueChanged (or TextChanged) event
> handler set, like:

> -----------

> System.Windows.Forms.TextBox cityTextBox;

> cityTextBox = new TextBox();
> cityTextBox.TextChanged += new EventHandler(cityTextBox_TextChanged);

> -----------

> I can loop through all of the controls of the application starting
> with this.Controls and, recursively their children, finding everyone
> of my 100 individual controls.

> I want my software to verify if I have every TextChanged or
> ValueChanged event handler set.

> Any clues?

> Thanks- Hide quoted text -

- Show quoted text -

Nicholas:

Yes, I have tried reflection as follows:

----------

using System.Reflection;

System.Type type = cityTextBox.GetType();

EventInfo eventInfo = type.GetEvent("TextChanged",
BindingFlags.Instance | BindingFlags.Public);

MethodInfo methodInfo = eventInfo.GetRaiseMethod(true);

//
// However, methodInfo always comes back null.
//
----------

Is the above code what you had in mind?

.



Relevant Pages

  • Re: RFC: Building the Perfect Tabbed Pane (an tutorial article)
    ... so the fact that the Safari workaround breaks if cancelBubble is used ... var wrapHandler = function{ ... var listener = createListener(element, 'on'+eventType, ...
    (comp.lang.javascript)
  • Re: Differentiating between horizontal and vertical table borders
    ... current location in the 4 directions (up, down, ... border, without touching the cells, you have to get the information ... I cannot think of anything better currently; using a mousemove handler ... var evt = window.event; ...
    (comp.lang.javascript)
  • Another (Re: Useful Javascript example)
    ... The Event Listener registry was based on the one in ... var listenerRegistry = function{ ... return function(element, eventName, handler) { ... element.addEventListener(eventName, handler, false); ...
    (comp.os.vms)
  • Re: I believe VS.NET is removing my code!!!!!!
    ... It's removing "Handles myControl.whatever_event" from the end of my handler. ... I must say this is the stupidest bug I have ever come across, and it seems like something that should have been a priority for the VS 2003 release. ... Rearranging controls on a form shouldn't cause your program to crash. ... > The button control raises an event, and my web form handles the event. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do "dirty checking" with a Windows Form?
    ... Yes you are right (my code only used one handler for each of the textboxes). ... > of a hassle if you have lots of controls, ... > are sending the change event to the parent without having to subscribe. ... >> private void InitializeComponent() ...
    (microsoft.public.dotnet.languages.csharp)