Overview
This article describes how to create a simple, Custom User Control-A smooth progress bar.
In the earlier version of the progress bar control, for example, the version provided in the Microsoft Windows Common Controls ActiveX control, you can see that the progress bar has two different views. You can set the Standard view or Smooth view by setting the Scrolling attribute. The Smooth view provides an area for Smooth display of the Progress. Standard tries to show that the progress is represented by a block.
The progress bar control provided in Visual C #. NET only supports Standard views.
The sample code in this article shows how to create a control with the following attributes:
Minimum. This attribute indicates the minimum value of the progress bar. The default value is 0. You cannot set this attribute to a negative value.
Maximum. This attribute indicates the maximum value of the progress bar. The default value is 100.
Value. This attribute indicates the current value of the progress bar. The value must be between Minimum and Maximum.
ProgressBarColor. This attribute indicates the color of the progress bar.
Create a custom progress bar Control
1. Follow these steps to create a Windows Control Library project in Visual C #. NET:
A. Open Microsoft Visual Studio. NET.
B. Click the File menu, New, and Project.
C. In the New Project dialog box, select Visual C # Projects in Project Types, and then select Windows Control Library in Templates.
D. In the Name box, fill in the SmoothProgressBar and click OK.
E. In Project Explorer, rename the default class module and change UserControl1.cs to SmoothProgressBar. cs.
F. In the Property Window of the UserControl object, change its Name attribute from UserControl1 to SmoothProgressBar.
2. Now, you have inherited a new class from the control class and can add new functions. However, the progress bar is sealed and cannot be inherited. Therefore, you must create this control from scratch.
Add the following code to the UserControl module after "Windows Form Designer generated code:
Int min = 0; // Minimum value for progress range Int max = 100; // Maximum value for progress range Int val = 0; // Current progress Color BarColor = Color. Blue; // Color of progress meter
Protected override void OnResize (EventArgs e) { // Invalidate the control to get a repaint. This. Invalidate (); }
Protected override void OnPaint (PaintEventArgs e) { Graphics g = e. Graphics; SolidBrush brush = new SolidBrush (BarColor ); Float percent = (float) (val-min)/(float) (max-min ); Rectangle rect = this. ClientRectangle;
// Calculate area for drawing the progress. Rect. Width = (int) (float) rect. Width * percent );
// Draw the progress meter. G. FillRectangle (brush, rect );
// Draw a three-dimen=border around the control. Draw3DBorder (g );
// Clean up. Brush. Dispose (); G. Dispose (); }
Public int Minimum { Get { Return min; }
Set { // Prevent a negative value. If (value <0) { Min = 0; }
// Make sure that the minimum value is never set higher than the maximum value. If (value> max) { Min = value; Min = value; }
// Ensure value is still in range If (val <min) { Val = min; }
// Invalidate the control to get a repaint. This. Invalidate (); } }
Public int Maximum { Get { Return max; }
Set { // Make sure that the maximum value is never set lower than the minimum value. If (value <min) { Min = value; }
Max = value;
// Make sure that value is still in range. If (val> max) { Val = max; }
// Invalidate the control to get a repaint. This. Invalidate (); } }
Public int Value { Get { Return val; }
Set { Int oldValue = val;
// Make sure that the value does not stray outside the valid range. If (value <min) { Val = min; } Else if (value> max) { Val = max; } Else { Val = value; }
// Invalidate only the changed area. Float percent;
Rectangle newValueRect = this. ClientRectangle; Rectangle oldValueRect = this. ClientRectangle;
// Use a new value to calculate the rectangle for progress. Percent = (float) (val-min)/(float) (max-min ); NewValueRect. Width = (int) (float) newValueRect. Width * percent );
// Use an old value to calculate the rectangle for progress. Percent = (float) (oldValue-min)/(float) (max-min ); OldValueRect. Width = (int) (float) oldValueRect. Width * percent );
Rectangle updateRect = new Rectangle ();
// Find only the part of the screen that must be updated. If (newValueRect. Width> oldValueRect. Width) { UpdateRect. X = oldValueRect. Size. Width; UpdateRect. Width = newValueRect. Width-oldValueRect. Width; } Else { UpdateRect. X = newValueRect. Size. Width; UpdateRect. Width = oldValueRect. Width-newValueRect. Width; }
UpdateRect. Height = this. Height;
// Invalidate the intersection region only. This. Invalidate (updateRect ); } }
Public Color ProgressBarColor { Get { Return BarColor; }
Set { BarColor = value;
// Invalidate the control to get a repaint. This. Invalidate (); } }
Private void Draw3DBorder (Graphics g) { Int PenWidth = (int) Pens. White. Width;
G. DrawLine (Pens. DarkGray, new Point (this. ClientRectangle. Left, this. ClientRectangle. Top ), New Point (this. ClientRectangle. Width-PenWidth, this. ClientRectangle. Top )); G. drawLine (Pens. darkGray, new Point (this. clientRectangle. left, this. clientRectangle. top), new Point (this. clientRectangle. left, this. clientRectangle. height-PenWidth )); G. DrawLine (Pens. White, new Point (this. ClientRectangle. Left, this. ClientRectangle. Height-PenWidth ), New Point (this. ClientRectangle. Width-PenWidth, this. ClientRectangle. Height-PenWidth )); G. DrawLine (Pens. White, new Point (this. ClientRectangle. Width-PenWidth, this. ClientRectangle. Top ), New Point (this. ClientRectangle. Width-PenWidth, this. ClientRectangle. Height-PenWidth )); }
|
3. Click Build Solution in the Build menu to compile the entire project.
Create a simple client application
1. Click New in the File menu, and then click Project.
2. In the Add New Project dialog box, click Visual C # Projects in Project Types, click Windows Application in Templates, and click OK.
3. follow the steps below to add two SmoothProgressBar instances to Form:
A. On the Tools menu, click Customize Toolbox.
B. Click the. NET Framework Components page.
C. Click Browse and select the SmoothProgressBar. dll file created in the Create a Custom ProgressBar Control Section.
D. Click OK. You can see that the SmoothProgressBar control already exists in the toolbox.
E. Drag two SmoothProgressBar control instances from toolbox to the default form in the Windows Application project.
4. Drag a Timer control from the toolbox page to form.
5. Add the following code to the Tick event of the Timer control:
If (this. smoothProgressBar1.Value> 0) { This. smoothProgressBar1.Value --; This. smoothProgressBar2.Value ++; } Else { This. timer1.Enabled = false; } |
6. Drag a Button control from the toolbox page to form