Differences and use of component class, user control, and custom control in winform

Source: Internet
Author: User

Net Framework provides you with the ability to develop and implement new controls. In addition to common user controls, you can now find that you can write custom controls that can execute your own drawings, or even extend the functions of existing controls by inheriting them. It may take some effort to determine which type of controls to create. This section focuses on the differences between various types of inherited controls and provides considerations related to the types selected for projects.

1. component class

Component class can inherit from any existing Windows Forms control to generate new controls. This method allows you to retain all of the inherent features of Windows Forms controls and then extend this feature by adding custom properties, methods, or other features. For example, you can createTextboxThe inherited control, which only accepts numbers and can automatically convert the input to a value. Such controls may contain verificationCodeThis code is called whenever the text in the text box is changed. In addition, the control may have an additional "value" attribute. In some controls, you can also overrideOnpaintMethod to add the custom appearance to the widget's graphical interface.

Inherit Windows Forms controls in the following situations:

    • Most of the required functions are already the same as existing Windows Forms controls.
    • You do not need to customize the graphic interface, or want to design a new graphic front-end for the existing control.

2. User Control

A user control is a collection of Windows Forms controls encapsulated in a public container. This container contains all inherent functions associated with each Windows Forms control, allowing you to selectively publish and bind their properties. One example of a user control may be a generated control that displays customer address data in the database. This control may contain several text boxes used to display fields, and several buttons used to record positioning. You can selectively publish data binding properties, package the entire control and apply itProgram.

Use in the following situationsUsercontrol:

    • You need to combine the functions of several Windows Forms controls into a reusable unit.

3. Custom Control

Custom ControlControlTo create a control from the ground up.ControlClass provides all the basic functions required by controls (such as events), but does not provide control-specific functions or graphical interfaces. Compared with creating a control by inheriting from a user control or an existing windows Form ControlControlIt takes a lot of effort and effort to create controls by using class inheritance. Because the author must be the control'sOnpaintEvent code writing and any function-specific code are required, but it also allows the author to flexibly customize and adjust the control based on their own needs. The clock control is a custom control that copies the appearance and operation of the simulated clock. The custom drawing will be called to push the clock pointer around to respond to the Internal timer component'sTickEvent.

Use custom control in the following situations:

    • You want to provide a custom graphical representation of the control.
    • You need to implement custom functions that cannot be obtained from standard controls.

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.