Why do I need to embed a component of the Windows.Forms constructor from the developer?

In C # (and Visual Basic), you can add several components other than the UI (those that are not inherited from System.Windows.Forms.Control ) directly from the form designer. Examples of these components are System.Windows.Forms.FolderBrowserDialog and System.Windows.Forms.Timer .

But what's the use of adding non-UI controls with a user interface designer instead of installing them directly from code? Is there any reason for this?

+5
source share
1 answer

I think that using components other than UI in the designer, at least, has the following advantages:

Development time support

One of the most powerful things you have in window forms is the ability to use the constructor to configure components.
Although Timer not a user interface component, you can set its properties as an interval at design time. This applies to many other components, such as BindingSource , ErrorProvider , ... that you can use very friendly property grids and types editors and type converters to set properties at design time.

  • If you want to configure dependent properties for other controls; for example, by adding a BindingSource to the constructor, it simplifies data binding.
  • If you need to use extension providers such as HelpProvider and Tooltip , as they are associated with other controls, they are very easy to configure in design mode.
  • When you need to configure properties such as DataSource and DataMember , it is very convenient to use the constructor and use the excellent grid properties.
  • Components will be added as class level fields, and you can make them public with deigner.
  • If you need to use the Localizable Form function for your components, it is fully accessible using the constructor.
  • If you just need to add or remove event handlers, you can do this using the property grid.

Standard code

If you look at the code created by the developer, you will see:

  • The generated code for components that support ISupportInitialize uses their BeginInit and EndInit
  • Generated code for components, pass this.components to the constructor, and then use it when Dispose

If you do not need development-time support, and you write standard code for components, so it’s quite normal to use a theme in the code.

+6
source

Source: https://habr.com/ru/post/1232362/


All Articles