Windows Develop Bookmark and Share   
 index > Windows Forms General > Professional progress bar question
 

Professional progress bar question

How to program a progress bar to load to its max and reload again ,by using visual studio C# 2008 ??
Heri Tony  Thursday, October 01, 2009 8:14 AM
You can develop your custom control inherited from ProgressBar.

Put a backgroundworker in the code and increase the value of progressbar in Dowork method og Backgroundworker.

Here is a sample.

    public class MyProgressBar : ProgressBar
    {
        System.ComponentModel.BackgroundWorker bgv = new System.ComponentModel.BackgroundWorker();
        public MyProgressBar()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            bgv.DoWork += new System.ComponentModel.DoWorkEventHandler(bgv_DoWork);
        }

        void bgv_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            while (true)
            {
                if (this.Value + IncrementValue > this.Maximum)
                {
                    if (this.Value + IncrementValue - this.Maximum < this.Minimum)
                    {
                        this.Value = this.Minimum;
                    }
                    else
                    {
                        this.Value = this.Value + IncrementValue - this.Maximum;
                    }
                }
                this.Value += IncrementValue;
                System.Threading.Thread.Sleep(IntervalBetveenIncrement);
            }
        }
        private int _IntervalBetveenIncrement = 500;

        public int IntervalBetveenIncrement
        {
            get { return _IntervalBetveenIncrement; }
            set { _IntervalBetveenIncrement = value; }
        }

        private int _IncrementValue = 25;

        public int IncrementValue
        {
            get { return _IncrementValue; }
            set { _IncrementValue = value; }
        }

        public void Start()
        {
            bgv.RunWorkerAsync();
        }
        public void Stop()
        {
            bgv.CancelAsync();
        }
        public bool IsWorking
        {
            get { return bgv.IsBusy; }
        }
    }
To run the control call it's start method. And to stop call stop method.
Note: This is a not well tested code. If you plan to use this code you should handle these situations before using in your project.Ex:Calling start twice may crash.
Tamer Oz  Thursday, October 01, 2009 9:04 AM
Can you be more specific as to what part you're trying to accomplish? Do you just need it to go back to zero when it hits max? Cause here's some code to do that:

        public class MyProgressBar : ProgressBar
        {
            public new void PerformStep()
            {
                if (this.Value == this.Maximum)
                    this.Value = this.Minimum;
                else
                    base.PerformStep();
            }
        }
ScottyDoesKnow  Thursday, October 01, 2009 2:11 PM
You can develop your custom control inherited from ProgressBar.

Put a backgroundworker in the code and increase the value of progressbar in Dowork method og Backgroundworker.

Here is a sample.

    public class MyProgressBar : ProgressBar
    {
        System.ComponentModel.BackgroundWorker bgv = new System.ComponentModel.BackgroundWorker();
        public MyProgressBar()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            bgv.DoWork += new System.ComponentModel.DoWorkEventHandler(bgv_DoWork);
        }

        void bgv_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            while (true)
            {
                if (this.Value + IncrementValue > this.Maximum)
                {
                    if (this.Value + IncrementValue - this.Maximum < this.Minimum)
                    {
                        this.Value = this.Minimum;
                    }
                    else
                    {
                        this.Value = this.Value + IncrementValue - this.Maximum;
                    }
                }
                this.Value += IncrementValue;
                System.Threading.Thread.Sleep(IntervalBetveenIncrement);
            }
        }
        private int _IntervalBetveenIncrement = 500;

        public int IntervalBetveenIncrement
        {
            get { return _IntervalBetveenIncrement; }
            set { _IntervalBetveenIncrement = value; }
        }

        private int _IncrementValue = 25;

        public int IncrementValue
        {
            get { return _IncrementValue; }
            set { _IncrementValue = value; }
        }

        public void Start()
        {
            bgv.RunWorkerAsync();
        }
        public void Stop()
        {
            bgv.CancelAsync();
        }
        public bool IsWorking
        {
            get { return bgv.IsBusy; }
        }
    }
To run the control call it's start method. And to stop call stop method.
Note: This is a not well tested code. If you plan to use this code you should handle these situations before using in your project.Ex:Calling start twice may crash.
Tamer Oz  Thursday, October 01, 2009 9:04 AM
Can you be more specific as to what part you're trying to accomplish? Do you just need it to go back to zero when it hits max? Cause here's some code to do that:

        public class MyProgressBar : ProgressBar
        {
            public new void PerformStep()
            {
                if (this.Value == this.Maximum)
                    this.Value = this.Minimum;
                else
                    base.PerformStep();
            }
        }
ScottyDoesKnow  Thursday, October 01, 2009 2:11 PM

You can use google to search for other answers

Custom Search

More Threads

• MDI children's child
• Browser control in WinForms UserControl questions
• Determine if the point is in the polygon, C#
• Logging into MDI Application
• Split Containers - Beta 2
• How can I get the Client Sock ID in my VB.NET App?
• Windows forms border referesh issue
• what are the prerequisites for a windows application developed in visual studio 2008 to run on xp machine?
• System.Net.Mail - How to know if the send message was read?
• Selected Item in a List Box