WinForm the difference between component Class, User control, and custom control and suggestions for use

Source: Internet
Author: User

reference:http://blog.csdn.net/redstonehe/article/details/1536549

The. NET Framework gives you the ability to develop and implement new controls. In addition to the common user controls, you will now find that you can write custom controls that can perform your own drawing, and even extend the functionality of existing controls through inheritance. Determining what type of control to create might be a contributory effort. This section focuses on the differences between the various inheritable controls, while providing considerations related to the types that are selected for the project.

1, Component Class

The Component class can inherit from any existing Windows forms control to produce a new control. This method allows you to preserve all the inherent functionality of a Windows forms control and then extend this intrinsic functionality by adding custom properties, methods, or other features. For example, you can create a control that inherits from a TextBox that accepts only numbers and can automatically convert the input to a value. Such a control might contain validation code that is called whenever the text in the text box changes, and the control may have an additional value property. In some controls, you can also add a custom skin to the control's graphics interface by overriding the OnPaint method of the base class.

Inherit the Windows forms control when the following conditions are in:

    • Most of the required features are already the same as existing Windows forms controls.
    • You do not need to customize the graphics interface, or you want to design a new graphical front end for existing controls.

2. User Control

A user control is a collection of Windows forms controls encapsulated within a common container. This container contains all of the inherent features associated with each Windows forms control, allowing you to selectively expose and bind their properties. One example of a user control might be a control that has been generated to display the customer address data in the database. The control may include several text boxes to display the fields, and several button controls that are used to position them through the record. You can selectively expose data-binding properties, and you can package the entire control and reuse it between applications.

Use UserControl in the following situations:

    • To synthesize the functionality of several Windows forms controls into a reusable unit.

3. Custom Control

Custom control creates a control entirely from the beginning, by inheriting from control . the control class provides all the basic functionality required for a control, such as an event, but does not provide a control-specific feature or graphics interface. Creating a control by inheriting from the control class requires much more thought and effort than creating a control by inheriting from a user control or an existing Windows forms control. Because the author must write code for the control's OnPaint event and any required feature-specific code, it also allows the author the flexibility to customize the adjustment control to suit his or her own needs. The clock control is a custom control that duplicates the appearance and operation of the analog clock, and the custom drawing is called to move the clock pointer around to respond to the Tick event of the internal timer component.

Use the custom Control in the following situations:

    • You want to provide a custom graphical representation of the control.
    • You need to implement custom features that you cannot get from standard controls.

Differences between component Class, User control, and custom control in WinForm and suggestions for use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.