Create a progress bar control in Visual Studio

Source: Internet
Author: User
Create a custom progressbar Control

  1. Follow these steps to create a new windows control library project in Visual C #:
    1. Start Microsoft Visual Studio.
    2. OnFileMenu, pointNew, And then clickProject.
    3. InNew projectDialog box, clickVisual C #UnderProject types, And then clickWindows Forms control libraryUnderTemplates.

      NoteIn Visual Studio. NET 2003, clickVisual C # ProjectsInsteadVisual C #.

    4. InNameBox, typeSmoothprogressbar, And then clickOK.
    5. In project explorer, rename the default class module from usercontrol1.cs to smoothprogressbar. CS.
    6. In the Properties window forUsercontrolObject, changeNameProperty fromUsercontrol1ToSmoothprogressbar.

Comment: these steps show us how to create a customized control.

 

2. At this point, you typically inherit from the class of that control and then add the additional functionality to extend an existing control. However,ProgressbarClass isSealedAnd cannot be inherited.Therefore, you must build the control from the beginning.

Commets: The deleted line seems to have no meaning at all. Please note the progressbar is sealed. A sealed class cannot be inherited.

Add the following code to the smoothprogressbar. CS file, in the class that is derived from usercontrol.

 

Int min = 0; // minimum value for progress rangeint max = 100; // maximum value for progress rangeint val = 0; // current progresscolor barcolor = color. blue; // color of progress meterprotected 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-dimen1_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;} // ensure value is still in rangeif (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 ));}

 

 

 

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.