欢迎光临
我们一直在努力

建站超值云服务器,限时71元/月

//——————————————————————————
/// <copyright from=1997 to=2001 company=microsoft corporation>
///    copyright (c) microsoft corporation. all rights reserved.
///
///    this source code is intended only as a supplement to microsoft
///    development tools and/or on-line documentation.  see these other
///    materials for detailed information regarding microsoft code samples.
///
/// </copyright>
//——————————————————————————
namespace microsoft.samples.windows.forms.cs.progressbarctl {
    using system;
    using system.collections;
    using system.componentmodel;
    using system.drawing;
    using system.windows.forms;
    using system.threading;

    // <doc>
    // <desc>
    //     this class demonstrates the progressbar control.
    //     the progressbar is updated periodically via another thread based on the
    //     settings in this control
    // </desc>
    // </doc>
    //
    public class progressbarctl : system.windows.forms.form {

        private system.componentmodel.container components;
        protected internal system.windows.forms.label label3;
        protected internal system.windows.forms.label lblcompleted;
        protected internal system.windows.forms.trackbar sldrspeed;
        protected internal system.windows.forms.progressbar progbar;
        protected internal system.windows.forms.label label5;
        protected internal system.windows.forms.groupbox grpbehavior;
        protected internal system.windows.forms.label label4;
        protected internal system.windows.forms.label label6;
        protected internal system.windows.forms.label lblvalue;
        protected internal system.windows.forms.combobox cmbstep;

        private int     isleeptime ;
        private thread  timedprogress ;

        public progressbarctl() : base() {
            //
            // required for win form designer support
            //
            initializecomponent();

            //
            // todo: add any constructor code after initializecomponent call
            //
            isleeptime = 100 ;
            cmbstep.selectedindex = 0 ;
            progbar.step = 1 ;

        }

        protected override void onload(eventargs e) {
            // spin off a new thread to update the progressbar control
            timedprogress = new thread(new threadstart(timedprogressproc));
            timedprogress.isbackground = true;
            timedprogress.start();
        }

        // <doc>
        // <desc>
        //     this code executes on the windows.forms thread.
        // </desc>
        // </doc>
        //
        private void updateprogress() {
            int min ;
            double numerator, denominator, completed ;

            //reset to start if required
            if (progbar.value == progbar.maximum) {
                progbar.value = progbar.minimum ;
            }
            else {
                
                progbar.performstep();
            }

            lblvalue.text = progbar.value.tostring();

            min         = progbar.minimum ;
            numerator   = progbar.value – min ;
            denominator = progbar.maximum – min ;
            completed   = (numerator / denominator) * 100.0 ;

            lblcompleted.text = math.round(completed).tostring() + "%" ;
        }

        // <doc>
        // <desc>
        //     this function runs in the timedprogress thread and updates the
        //     progressbar on the form.
        // </desc>
        // </doc>
        //
        private void timedprogressproc() {
            try {
                methodinvoker mi = new methodinvoker(updateprogress);
                while (true) {
                    invoke(mi);
                    int isleeptime = this.sleeptime;
                    thread.sleep(isleeptime) ;
                }
            }
            //thrown when the thread is interupted by the main thread – exiting the loop
            catch (threadinterruptedexception e) {
                if (e != null) {}
            }
            catch (exception we) {
                if (we != null) {
                    messagebox.show(we.tostring());
                }
            }
        }

        // <doc>
        // <desc>
        //     property controlling the progress of the progress bar – used by the background thread
        // </desc>
        // </doc>
        //
        private int sleeptime {
            get {
                lock(this) {
                    return isleeptime ;
                }
            }
            set {
                lock(this) {
                    isleeptime = value ;
                }
            }
        }

        /// <summary>
        ///    clean up any resources being used
        /// </summary>
        public override void dispose() {
            /*
             * we have to make sure that our thread doesnt attempt
             * to access our controls after we dispose them.
             */
            if (timedprogress != null) {
                timedprogress.interrupt();
                timedprogress = null;
            }

            base.dispose();
            components.dispose();
        }

        protected void sldrspeed_scroll(object sender, eventargs e) {
            trackbar tb = (trackbar) sender ;
            int time = 110 – tb.value ;
            this.sleeptime = time ;
        }

        protected void cmbstep_selectedindexchanged(object sender, eventargs e) {
            try {
                progbar.step = int32.parse((string)cmbstep.selecteditem);
            }
            catch (exception ex) {
                // thrown if int32.parse cant convert
                if (ex !=null) {}
            }
        }

        /// <summary>
        ///    required method for designer support – do not modify
        ///    the contents of this method with the code editor
        /// </summary>
        private void initializecomponent() {
            this.lblvalue = new system.windows.forms.label();
            this.label4 = new system.windows.forms.label();
            this.label5 = new system.windows.forms.label();
            this.progbar = new system.windows.forms.progressbar();
            this.grpbehavior = new system.windows.forms.groupbox();
            this.cmbstep = new system.windows.forms.combobox();
            this.label3 = new system.windows.forms.label();
            this.sldrspeed = new system.windows.forms.trackbar();
            this.label6 = new system.windows.forms.label();
            this.lblcompleted = new system.windows.forms.label();
            this.grpbehavior.suspendlayout();
            ((system.componentmodel.isupportinitialize)(this.sldrspeed)).begininit();
            this.suspendlayout();
            //
            // lblvalue
            //
            this.lblvalue.location = new system.drawing.point(164, 93);
            this.lblvalue.name = "lblvalue";
            this.lblvalue.size = new system.drawing.size(72, 18);
            this.lblvalue.tabindex = 4;
            //
            // label4
            //
            this.label4.location = new system.drawing.point(20, 93);
            this.label4.name = "label4";
            this.label4.size = new system.drawing.size(288, 18);
            this.label4.tabindex = 0;
            this.label4.text = "completion speed:";
            //
            // label5
            //
            this.label5.location = new system.drawing.point(31, 65);
            this.label5.name = "label5";
            this.label5.size = new system.drawing.size(143, 28);
            this.label5.tabindex = 1;
            this.label5.text = "percent completed:";
            //
            // progbar
            //
            this.progbar.backcolor = system.drawing.systemcolors.control;
            this.progbar.location = new system.drawing.point(31, 28);
            this.progbar.name = "progbar";
            this.progbar.size = new system.drawing.size(245, 18);
            this.progbar.step = 1;
            this.progbar.tabindex = 0;
            this.progbar.text = "progbar";
            //
            // grpbehavior
            //
            this.grpbehavior.controls.addrange(new system.windows.forms.control[] {
                                                                                      this.cmbstep,
                                                                                      this.label3,
                                                                                      this.sldrspeed,
                                                                                      this.label4});
            this.grpbehavior.location = new system.drawing.point(317, 19);
            this.grpbehavior.name = "grpbehavior";
            this.grpbehavior.size = new system.drawing.size(318, 175);
            this.grpbehavior.tabindex = 5;
            this.grpbehavior.tabstop = false;
            this.grpbehavior.text = "progressbar";
            //
            // cmbstep
            //
            this.cmbstep.dropdownstyle = system.windows.forms.comboboxstyle.dropdownlist;
            this.cmbstep.dropdownwidth = 123;
            this.cmbstep.items.addrange(new object[] {
                                                         "1",
                                                         "5",
                                                         "10",
                                                         "20"});
            this.cmbstep.location = new system.drawing.point(174, 28);
            this.cmbstep.name = "cmbstep";
            this.cmbstep.size = new system.drawing.size(123, 20);
            this.cmbstep.tabindex = 7;
            this.cmbstep.selectedindexchanged += new system.eventhandler(this.cmbstep_selectedindexchanged);
            //
            // label3
            //
            this.label3.location = new system.drawing.point(20, 28);
            this.label3.name = "label3";
            this.label3.size = new system.drawing.size(62, 18);
            this.label3.tabindex = 6;
            this.label3.text = "step:";
            //
            // sldrspeed
            //
            this.sldrspeed.backcolor = system.drawing.systemcolors.control;
            this.sldrspeed.location = new system.drawing.point(20, 111);
            this.sldrspeed.maximum = 100;
            this.sldrspeed.minimum = 10;
            this.sldrspeed.name = "sldrspeed";
            this.sldrspeed.size = new system.drawing.size(277, 42);
            this.sldrspeed.tabindex = 1;
            this.sldrspeed.tabstop = false;
            this.sldrspeed.text = "trackbar1";
            this.sldrspeed.tickfrequency = 10;
            this.sldrspeed.value = 10;
            this.sldrspeed.scroll += new system.eventhandler(this.sldrspeed_scroll);
            //
            // label6
            //
            this.label6.location = new system.drawing.point(31, 93);
            this.label6.name = "label6";
            this.label6.size = new system.drawing.size(128, 18);
            this.label6.tabindex = 3;
            this.label6.text = "value:";
            //
            // lblcompleted
            //
            this.lblcompleted.location = new system.drawing.point(164, 65);
            this.lblcompleted.name = "lblcompleted";
            this.lblcompleted.size = new system.drawing.size(72, 18);
            this.lblcompleted.tabindex = 2;
            //
            // progressbarctl
            //
            this.autoscalebasesize = new system.drawing.size(6, 14);
            this.clientsize = new system.drawing.size(647, 202);
            this.controls.addrange(new system.windows.forms.control[] {
                                                                          this.grpbehavior,
                                                                          this.lblvalue,
                                                                          this.label6,
                                                                          this.lblcompleted,
                                                                          this.label5,
                                                                          this.progbar});
            this.formborderstyle = system.windows.forms.formborderstyle.fixeddialog;
            this.maximizebox = false;
            this.minimizebox = false;
            this.name = "progressbarctl";
            this.text = "progressbar";
            this.load += new system.eventhandler(this.progressbarctl_load);
            this.grpbehavior.resumelayout(false);
            ((system.componentmodel.isupportinitialize)(this.sldrspeed)).endinit();
            this.resumelayout(false);

        }

        // the main entry point for the application.
        [stathread]
        public static void main(string[] args) {
            application.run(new progressbarctl());
        }

        private void progressbarctl_load(object sender, system.eventargs e)
        {

        }

    }

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
分享到: 更多 (0)

相关推荐

  • 暂无文章