Hi,
I have a form with many controls, each having events (and their handlers), total some tens events .
I found out that many times, due to complex combinations, events are being fired while the controls are being initiated, mainly because I load saved settings from my settings file that may change the default controls' initial settings, causing events to fire.
To avoid this, I moved all my events to a special method (in Main) that is being called only after all controls have been built and and set.
It works fine, but the question is if this is good or common practice and what drawbacks it may have.
I have also tried to move the events to a special Maim subclass, but could not find a way to get access to the private controls from the subclass.
Loading
VulpesPosted Apr 1, 2014, 7:33 AM
button1.Click += button1_Click;
private void button1_Click (object sender, EventArgs e)
{
// add your code
}
If so, then it's not a common practice to do this with design time controls though it's the only option when adding controls dynamically.
As regards whether it's a good practice or not, then it sounds like it's the only thing you can do to prevent certain events firing prematurely.
Apart from the additional coding required, there are no drawbacks. After all, you're only mimicking what the VS designer does anyway :)
In the Properties Box, you'd have to change the Modifiers property of the controls from private to protected to access them from a subclass of your form. if it's working OK now, I don't think I'd bother trying to change it.
SamPosted Apr 1, 2014, 10:23 AM