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 948695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:17:04+00:00 2026-05-15T23:17:04+00:00

Is it possible in VB.NET to easily write an event handler that will handle

  • 0

Is it possible in VB.NET to easily write an event handler that will handle every event that fires? I’m wondering if one could make a logging system using something like this.

I’m wanting to do something like (in pseudocode):

Public Sub eventHandledEvent(ByVal sender As Object, ByVal e As EventArgs)
    File.Write(sender.EventName)
End Sub

I realize it would be slow, but it wouldn’t be for a production system, only as a development tool.

  • 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-15T23:17:05+00:00Added an answer on May 15, 2026 at 11:17 pm

    You can do this with reflection. Here’s how. Create a form with a textbox called TextBox1. Paste the following code. Run the project and look at the immediate window.

    Public Class Form1
    
      Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        RegisterAllEvents(TextBox1, "MyEventHandler")
      End Sub
    
      Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
        Debug.WriteLine("An event has fired: sender= " & sender.ToString & ", e=" & e.ToString)
      End Sub
      Sub RegisterAllEvents(ByVal obj As Object, ByVal methodName As String)
        'List all events through reflection'
        For Each ei As System.Reflection.EventInfo In obj.GetType().GetEvents()
          Dim handlerType As Type = ei.EventHandlerType
          Dim method As System.Reflection.MethodInfo = Me.GetType().GetMethod(methodName)
          'Create a delegate pointing to the method'
          Dim handler As [Delegate] = [Delegate].CreateDelegate(handlerType, Me, method)
          'Register the event through reflection'
          ei.AddEventHandler(obj, handler)
        Next
      End Sub
    End Class
    

    This is from Francesco Balena’s book Programming Microsoft Visual Basic 2005 The Language. The techique works with any object that raises events, not just controls. It uses contravariance.

    If you buy the book, there’s a full explanation and some more code which will allow you to identify which event has fired in the universal handler, and use regular expressions to handle only a subset of events. I don’t feel I can post such a long excerpt here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: VB.NET RemoveHandler & Anonymous Methods If I add an event handler like
I am wondering whether there are libraries in .NET that make it easy to
Possible Duplicate: ASP.Net:Best way to run scheduled tasks I have to make a scheduled
I only want facts as far as possible. There are some things that ASP.Net
I was reading http://www.orchardproject.net/docs/Creating-1-n-and-n-n-relations.ashx and could not get the idea, if it is possible
Possible Duplicate: ASP.NET MVC quick start - a one-stop tutorial? I am starting a
Is this possible in .net to see visually ,thread structure those are running (in
Possible Duplicate: ASP.NET MVC3 how to excute action method of controller using timer with
Possible Duplicate: Protect .NET code from reverse engineering? we just develop a application with
Possible Duplicate: ASP.NET “special” tags What is the difference between <%# ... %> ,

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.