Reputation: 425
I've red that those methods of ISupportInitialize
interface are used by Designer to support optimization, to ensure atomicity of initialization of controls, and to prevent any action on controls during initialization. My questions are:
Upvotes: 13
Views: 8979
Reputation: 942247
It does not have anything to do with optimization. ISupportInitialize
is an interface that you need when your control is sensitive to the order in which properties are assigned. There isn't any way to affect the order in which the designer assigns them, it does it alphabetically.
You typically set a bool variable to true in your BeginInit()
method, you test this in the property setters and don't do anything when it is set. Your EndInit()
method then makes the property values effective.
You can see a good example of this in the ErrorProvider component. Note how it uses the methods to defer data binding. The PictureBox control is another good example, it uses it to defer image downloading. TrackBar is yet another example, it uses it to ensure that the Value
property is between Minimum and Maximum. Etcetera, the .NET Framework source is often a very good place to see how .NET types are used in practice.
Upvotes: 12
Reputation: 1186
By definition the Designer permits to create controls in a visual mode, the appropriate code to initialize the controls is then generated by Visual Studio.
The initialization is done in one place to avoid any null reference issue later in your code. You expect indeed every controls already created when you use their reference.
You perfectly can create your controls initialization if for example you want to dynamically create your interface based on a specific constructor.
Upvotes: -1