Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

c# - Why should I insert a non-UI Windows.Forms component from the designer?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
  • Standard code (for initialize and dispose)

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.

  • When you want to configure dependent properties for other controls; for example by adding BindingSource to designer, it makes data binding very easy.
  • When you need to use extender providers like HelpProvider and Tooltip, since they are related to other controls, it is very easy to configure them at design mode.
  • When you need to configure properties like DataSource and DataMember, it is very friendly to use designer and use great property grid features.
  • Components will add as class level fields, and you can make them public using deigner.
  • When you need to use Localizable feature of Form for your components, it's completely available using designer.
  • When you need to simply add or remove event handlers you can do it using property grid.

Standard Code

If you take a look at designer generated code you will see:

  • Generated code for components that supports ISupportInitialize uses their BeginInit and EndInit
  • Generated code for components, pass 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...