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.