In C# (and Visual Basic) you can add several non-UI components (those that don't inherit from System.Windows.Forms.Control
) directly from the form designer. Examples of those components are System.Windows.Forms.FolderBrowserDialog
and System.Windows.Forms.Timer
.
But what's the benefit from adding non-UI controls with the UI designer instead of instancing them directly from code? There's a rationale behind that?
The Windows Forms Designer shows the designer surface for the DemoCalculator control. In this view, you can graphically design the appearance of the control by selecting controls and components from Toolbox and placing them on the designer surface.
The Form Designer gives you an object-oriented, visual environment for creating graphical forms and other user interface elements. You can create a form using either the Form Wizard or the Form Designer without the wizard. You can also create subforms to include in your forms.
Try using Application. EnableVisualStyles() . Make sure to call it before Application. Run() .
If you don't need design-time support and if you yourself will take care of writing standard code to initialize and dispose the component, then you don't need to drop component on designer.
Using non-UI components in designer has these benefits:
Design-Time Support
One of the most powerful things that you have in windows forms is the ability to use designer to set Component properties.
Even though Timer
is not a UI component, but you can set its properties like interval at design time. This applies to many other components like BindingSource
, ErrorProvider
, and etc that you can use very friendly property grid and type editors and type converters to configure properties at design time.
BindingSource
to designer, it makes data binding very easy.HelpProvider
and Tooltip
, since they are related to other controls, it is very easy to configure them at design mode.DataSource
and DataMember
, it is very friendly to use designer and use great property grid features.Localizable
feature of Form
for your components, it's completely available using designer.Standard Code
If you take a look at designer generated code you will see:
ISupportInitialize
uses their BeginInit
and EndInit
this.components
to constructor and then use it when Dispose
If you don't need design-time support and you write standard code for components, so it's completely OK to use theme in code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With