问题
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:
WinForms equivalent of ASP.NET page load event (Bytes)
WinForms equivalent of ASP.NET page load event (PC Review)
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
- 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.
- 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.
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/12174148/alternative-to-pageload-in-windows-forms