Friday, June 24, 2005

Wiring up User Interface Events in C#

One of the things I really l like about C# is the way events work. I was already accustomed to creating event listeners and adding them to the event source in Java, so it was a no brainer to switch to C#. One thing that was a little confusing at first, though, was how to easily wire up my events for user interface components. In VB6 and VB.NET it was simple enough to just select the event from the drop down list at the top of the code window. Unfortunately, the drop down windows in the C# code window down show events, only the methods already implemented.

Fortunately, it is just a matter of looking in a different place. If you are looking at your C# code page, hit Shift-F7 to flip back to designer mode. Select the component you want to wire up an event for. Now look at the properties window (if you don't have it up, go to the View menu and select it). Notice the little lightning bolt icon in the toolbar? Click this and you will get to the list of public events exposed by the control. If you double click an event, the IDE will automatically insert the code skeleton and wire up the event. If you already have an event handler written, just click the arrow to get a dropdown list of all of the functions that can listen to the event.

Property list for a Windows Form (events button circled)

Event list for a Windows Form


  1. I don't suppose you know how to fetch the event viewer logs from a remote server? I hear rumors it can be done through the .net framework.

    although at this point the only VB I've written was a program that takes three numbers as input, and averages them when you click the "start" button.

  2. My brother worked for a company that made a product to do exactly that. Unfortunately, that company is now defunct. It was called Security Profiling, but I'm not sure if the products are even available under a different name now.