1

I have many events on a Windows Forms form. I have a feedback label, and I have to write feedbaxk.Text=""; at the beginning of each event.

In ASP.NET, I have been using to write it only once in the page_load event which is fired on every postback, but Form Load fires only once.

I have tried to search with the title of my question and found following two closest answers:

There is some relevant material, but I could not get satisfying answer from these and many more.

How it can be detected in Windows Forms that an event is going to be fired?

Update: By googling it another way, I found a close link which is linked with another links, Find all event handlers for a Windows Forms control in .NET and Is it possible to “steal” an event handler from one control and give it to another?. After viewing these I think it is possible if

  1. I could get list of all events in a win-form say at the end of Form_Load (after that I don't add any more events). Then add a little common functionality (say changing a label text) to all events.
  2. I might be able to inherit all of my events from a parent one and add my desired functionality in that event and then add (override) specific functionality to each event.
Community
  • 1
  • 1
Sami
  • 8,168
  • 9
  • 66
  • 99
  • 1
    Many events (not all) are raised due to underlying Windows messages arriving at the Form window handle, so you could override the protected Form.WndProc method and try to write some catch-all system. Depending on what you want to ultimately achieve, that could be a start. – Simon Mourier Sep 09 '12 at 17:11
  • I second what @SimonMourier said. Hooking to WndProc should be enough for what you need. – Marcel N. Sep 09 '12 at 21:33
  • I am working on SimonMourier guidance. @Lief! it is not just about buttonclick it is about all events whatever added e.g. a textbox_leave a combobox_selectedIndex_Changed a textbox_backgroudColorChanged and so on... – Sami Sep 15 '12 at 15:59
  • By googling it an other way i found a close link which is linked with another link .... http://stackoverflow.com/questions/3855985/find-all-event-handlers-for-a-winforms-control-in-net ------ http://stackoverflow.com/questions/293007/is-it-possible-to-steal-an-event-handler-from-one-control-and-give-it-to-anoth ----- @HansPassant. You have answered bit similar questions. See my Update. I am expecting that you can answer this question best – Sami Oct 30 '12 at 13:22

3 Answers3

2

It depends on any specific events you want to handle, because in your window you can have events for Resize, Click, Move, etc. Looking at the point you have raised with reference to ASP.NET, it seems you want to handle this on some button click.

You can try to override WndProc and handle the WM_COMMAND message. If you want to handle this on some specific button click then I would say you handle this for each button.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • +1. Just override WndProc. You'll find there's a hell of a lot more going on than you get in ASP.NET postbacks, so you'll need to filter the messages quite specifically to "sip from the firehose" so you only have to deal with stuff you care about. – tomfanning Sep 12 '12 at 16:08
  • Right. I try it. As required in question a common event is needed for all the events. Mean there will no event on which that commont event will not be fired, weather a selectedindexchanged of a combobox or a button clicked or so on – Sami Sep 12 '12 at 16:32
0

You can do this using AOP. Use the Unity (Unity Application Block) framework to decorate all your event method in your Windows forms form. Call your custom method before the event method call.

It is easy to do and hide code using reflection and the proxy pattern. A relevant article is Aspect-Oriented Programming, Interception and Unity 2.0.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17
  • Well. Thanks for the guidance I will study these things and try and update because at the moment I do not know these things. It seems a tough thing to me. – Sami Aug 29 '12 at 09:11
  • I understand but it is the only way to do it properly. You can also associate all windows form event to the same method but it will be very hard (in the method like a controller, you will have to detect which control and which event to route it on the good method with a big switch case, it is hard and very bad)... – Hassan Boutougha Aug 29 '12 at 09:15
  • aop is the answer to all transverse process (technical log, security, ...). one day will be sufficient to download unity and set aop, you will see it is easy ;-) – Hassan Boutougha Aug 29 '12 at 09:19
0

If you want to run some code on all button click events, you could create a helper method like the following which attaches a method to the click event handler for all the buttons on a form.

public static class UIHelper
{
    public static void RegesterButtonClicks(Form form, EventHandler method)
    {
        foreach (Control control in form.Controls)
        {
            if (control is Button)
            {
                control.Click += method;
            }
        }
    }
}

Then create your method to run on all button clicks. Something like this:

private void OnButtonClick(object sender, EventArgs e)
{
    //Code for all clicks here
}

And then add the following line of code under the InitializeComponent(); line in the constructor

UIHelper.RegesterButtonClicks(this, OnButtonClick);

You could also extend the helper method to attach the method to other events and other types of controls as well if you needed.

Lief
  • 535
  • 3
  • 5
  • 14