Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 566185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:57:03+00:00 2026-05-13T12:57:03+00:00

I want to set an event handler only if this is not set: If

  • 0

I want to set an event handler only if this is not set:

If GetHandlers(MyWindow.Closed, AddressOf MyWindow_Closed).Length = 0 Then
    AddHandler MyWindow.Closed, AddressOf MyWindow_Closed
EndIf
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:57:03+00:00Added an answer on May 13, 2026 at 12:57 pm

    You can’t really query the current value of the event’s delegate, except in the code that defines the event. What is your intent here? Normally you shouldn’t be too concerned (necessarily) with other subscribers? There are ways of hacking past the encapsulation to find the current value, but they are not recommended (it just isn’t a good idea).

    If your concern is whether you are already handling that event with that handler (i.e. you don’t want to double-subscribe, then you can always either a: fix the code so it doesn’t do this, or b: cheat (C# example):

    // remove handler **if subscribed**, then re-subscribe
    myWindow.Closed -= MyWindow_Closed;
    myWindow.Closed += MyWindow_Closed;
    

    To get the invocation list is… brittle but doable. In simple cases you can just use reflection to get the field, and snag the value. But with forms etc it uses sparse techniques (to minimise the space for events without subscribers). In the case of FormClosed, this is keyed via EVENT_FORMCLOSED.

    It might make more sense with an example (C#, sorry):

        Form form = new Form();
        form.FormClosed += delegate { Console.WriteLine("a");}; // just something, anything
        form.FormClosed += delegate { Console.WriteLine("b");}; // just something, anything
        object key = typeof(Form).GetField("EVENT_FORMCLOSED",
            BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
        EventHandlerList events = (EventHandlerList )
            typeof(Component).GetProperty("Events",
            BindingFlags.NonPublic | BindingFlags.Instance).GetValue(form, null);
        FormClosedEventHandler handler = (FormClosedEventHandler)events[key];
        foreach (FormClosedEventHandler subhandler in handler.GetInvocationList())
        {
            subhandler(form, null); // access the two events separately
        }
    

    In the case of an ObservableCollection<T>, the delegate is directly on a field, so less indirection is required:

    ObservableCollection<SomeType> list = ...
    NotifyCollectionChangedEventHandler handler = (NotifyCollectionChangedEventHandler)
        list.GetType()
        .GetField("CollectionChanged", BindingFlags.Instance | BindingFlags.NonPublic)
        .GetValue(list);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set registry keys in following way if OS is Vista then
I want to set something up so that if an Account within my app
I want to set all the fields and labels on a VFP7 report to
I want to set a background image for a div, in a way that
I want to set a breakpoint on the __DoPostBack method, but it's a pain
I want to set up a Subversion server installation on Windows Server 2003 64-bit
I want to set some attributes just before the object is serialized, but as
I want to set up an automatic rsync job to backup a bunch of
I want to set up an ASP.NET custom control such that it has a
I want to set the backgroun color for a GridViewColumn that is databound inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.