C # control development [entry]

Source: Internet
Author: User

The emergence of C # controls greatly improves the reuse of Interface UI operations and has positive significance for the improvement of development efficiency, but many times. net frame control and third-party control are difficult to meet our own development needs, this time we can use. to develop your own controls. You can even add the VS toolbox to get your own requirements by dragging as you like.

To develop a C # control, you need to first understand what controls are and what are the basic principles of control implementation. Then, based on these principles, we conduct development practices for several controls, we can develop our own controls.

 

First, what are controls?

Controls are actually a group of fields or methods that are inherited from System. Winddows. Controls or its subclass and can be reused by dragging them directly. (The above concepts are summarized based on my own understanding. If there is any inaccuracy, you are welcome to make a picture ).

From the definition of the control, we obtain that the control needs to be developed from the Controls or its subclass. In. net development, the WinForm control developed by myself usually has three types: Composite Controls, Extended Controls, and Custom Controls ).

Composite Control: Combines various existing controls to form a new control and centralize the functions of the centralized control.
Extended Control: A new control is derived from the existing control to add new functions or modify the control capabilities of the original control.
Custom Controls: Directly derived from the System. Windows. Forms. Control class. The Control class provides all the basic functions required by controls, including keyboard and mouse event processing. Custom Controls are the most flexible and powerful method, but have high requirements on developers. You must write code for the OnPaint event of the Control class. Therefore, at least you need to understand and learn how to use GDI +. You can download a simple GDI + learning material to guide you, for more advanced C # GDI + programming, you can refer to relevant materials.

 

Second, the principle of control implementation.

In this series of articles, we mainly explain the UI form controls. The basic principles of these controls are to expose some attributes, methods, and events to users, then, the user interacts with the control through these series of attributes, methods, and events. The control executes the OnPaint (PaintEventArgs e) method and passes the result through the GDI + or other actions, displayed on the form (control.

Through the control implementation principle, we know that to use the control development, we need to implement some public methods, events, attributes, and onpainting methods.

 

Based on our understanding of the control and control principles, we can make a simple widget. To understand the implementation of the control at the code level.

Purpose: we create a Label-like control to display the Text through Text settings:

First, we will create a new control class Library:

After selecting the Windows Forms control library, a user control is provided by default. Because we want to develop a custom control, we can delete this file.

 

In the open file, right-click the file and choose view code: Because the code is very simple, it will not be long-winded and will be pasted with comments:

 Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. text; <br/> using system. windows. forms; </P> <p> namespace newlabel <br/>{< br/> Public partial class newlabel: Control <br/>{< br/> Public newlabel () <br/>{< br/> initializecomponent (); <br/>}< br/> private string _ text = ""; <br/> private graphics g; </P> <p> [description ("content"), category ("appearance")] <br/> Public New String text <br/>{< br/> get {return _ text ;}< br/> set {_ text = value ;} <br/>}</P> <p> protected override void onpaint (painteventargs PE) <br/>{< br/> base. onpaint (PE); <br/> G = This. creategraphics (); <br/> G. drawstring (_ text, this. font, new solidbrush (color. black), new pointf (0, 0); <br/>}< br/> Public void dispose () {<br/> G. dispose (); <br/>}< br/>

The above code is implemented in vs. 2008.

 

After compilation, we can add a test project.

 

 

You can test the desired effect.

 

 

 

If you are reading this article, it means that you are a technology. You are not willing to miss this article.

Http://blog.csdn.net/aofengdaxia/archive/2010/10/15/5944193.aspx

 

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.