Key Points of user control design: System. Windows. Forms. UserControl

Source: Internet
Author: User

Key Points of user control design: System. Windows. Forms. UserControl
Key Points of User Control Design

A waterfall chart (color chart) function in a recent project is to present the point values in space and time in the form of graphs, such:

X coordinates are space, and a pixel in the horizontal direction represents a space unit (for example, 50 meters)

Y coordinates are time, and a pixel in the vertical direction represents a time unit (for example, 1 second)

Since there is no ready-made color chart control available, I tried to write a user control. This blog is about how to compile a similar user control.

 

The user control uses the self-drawing function. In fact, it is easier to implement self-drawing. follow these steps:

1. Create a class that inherits from System. Windows. Forms. UserControl. Right-click the solution project, select "add" and "user control", and enter the control name.

public partial class ColorControl : UserControl

2. Set the control DoubleBuffered to true to perform dual-Cache processing on the control to reduce the blinking of the Control During painting.

3. Add the following code to the constructor:

Set the ResizeRedraw value of the Control class to true, and cancel the tail dragging when the Control is adjusted (the previously drawn content is not erased)

//// Summary: // gets or sets a value that indicates whether the control redraws itself when it is adjusted. //// Return result: // If the widget redraws itself when it is adjusted, the value is true; otherwise, the value is false. Protected bool ResizeRedraw {get; set ;}
        public ColorControl()        {            InitializeComponent();            ResizeRedraw = true;        }

4. Override the OnPaint method of the control.

Protected override void OnPaint (PaintEventArgs e) {base. OnPaint (e); // you can Draw your own code to Draw (e. Graphics, this. ClientRectangle );}

How to write the Draw method depends on the project's requirements for controls. The following is a simple Draw method:

 

Of course, there are still many things to consider when creating chart controls. The most basic thing is to draw the coordinate system, draw the title, draw the main content area, and provide some events and methods for the customer class. For more information, see the open-source ZedGraph.

 

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.