Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The implementation method of the c-progress bar


May 12, 2021 C#



ProgressBar is often used when we use c- for WinFrom development. So how do you achieve winfrom progress bars and progress information tips?


Method one: Multithreaded


C-multithreaded tutorial


To achieve the progress bar effect, you need to use multithreaded, and if you don't use multithreaded to control the progress bar, the window can easily die (you can't see the progress information at the right time).

1, design interface, pay attention to the need to refer to using System.Threading;

The implementation method of the c-progress bar

The control names are:

progressBar1;label1;textBox1;button1;

2, define a proxy to update the value of ProgressBar and return the processing information of the method when executing it.

        private delegate void SetPos(int ipos,string vinfo);//代理

3. Progress bar value update function (arguments must be the same as declared proxy parameters)

        private void SetTextMesssage(int ipos,string vinfo)
        {
            if (this.InvokeRequired)
            {
                SetPos setpos = new SetPos(SetTextMesssage);
                this.Invoke(setpos, new object[] { ipos,vinfo });
            }
            else
            {
                this.label1.Text = ipos.ToString() + "/1000";
                this.progressBar1.Value = Convert.ToInt32(ipos);
                this.textBox1.AppendText(vinfo);
            }
        }

4, function implementation

private void button1_Click(object sender, EventArgs e)

        {
            Thread fThread = new Thread(new ThreadStart(SleepT));
            fThread.Start();
        }

5, the new thread execution function:

        private void SleepT()
        {
            for (int i = 0; i < 500; i++)
            {
                System.Threading.Thread.Sleep(10);
                SetTextMesssage(100*i/500,i.ToString()+"\r\n");
            }
        }

Program run effect map:

The implementation method of the c-progress bar


Method 2: Implemented by the control backgroundWorker1

1, the main form design:

The implementation method of the c-progress bar

Control name:

button1; b ackgroundWorker1;

For backgroundWorker1 controls, property settings:

The implementation method of the c-progress bar

2, the main page background code:

using System.Threading;// Refers to the space name

        private void button1_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.RunWorkerAsync(); // 运行 backgroundWorker 组件
            ProcessForm form = new ProcessForm(this.backgroundWorker1);// 显示进度条窗体
            form.ShowDialog(this);
            form.Close();
        }
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
            }
            else
            {
            }
        }
        //你可以在这个方法内,实现你的调用,方法等。
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);
                worker.ReportProgress(i);
                if (worker.CancellationPending)  // 如果用户取消则跳出处理数据代码 
                {
                    e.Cancel = true;
                    break;
                }
            }
        }

Select the events for the button control and the backgroundWorker1 control, respectively.

3. Set up sub-forms (and forms that display progress bars):

The implementation method of the c-progress bar

Control name:

progressBar1; b utton1

4, sub-form, background code:

private BackgroundWorker backgroundWorker1; ProcessForm Form Event (Progress Bar Form)

        public ProcessForm(BackgroundWorker backgroundWorker1)
        {
            InitializeComponent();
            this.backgroundWorker1 = backgroundWorker1;
            this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        }
        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //this.Close();//执行完之后,直接关闭页面
        }
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.CancelAsync();
            this.button1.Enabled = false;
            this.Close();
        }
    }

Just pick the event for button

The execution effect is:

The implementation method of the c-progress bar

The above is the introduction of the method of implementing winfrom progress bar and progress information tips, I hope to help you.