Winform C# 简单实现子窗口显示进度条

主窗口代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProgressbarTest
{
    public partial class Form1 : MaterialSkin.Controls.MaterialForm
    {
        public Form1()
        {
            InitializeComponent();
            Bkg_ClaculateStatus.WorkerReportsProgress = true;
            Bkg_ClaculateStatus.WorkerSupportsCancellation = true;
            Bkg_ClaculateStatus.DoWork += DoWork_Handler;
            Bkg_ClaculateStatus.ProgressChanged += ProcessChanged_Handler;
            Bkg_ClaculateStatus.RunWorkerCompleted += RunWorkerCompleted_Handler;

        }

        ProgressBar progressBar= new ProgressBar();
        private void materialFlatButton1_Click(object sender, EventArgs e)
        {
            if (!Bkg_ClaculateStatus.IsBusy)
            {
                Bkg_ClaculateStatus.RunWorkerAsync();
                progressBar.StartPosition = FormStartPosition.CenterParent;
                progressBar.ShowDialog();
            }
        }

        /// <summary>
        /// Use less variables to implement Fibonacci
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        static int Fn2(int n)
        {
            if (n <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            int a = 1;
            int b = 1;

            for (int i = 3; i <= n; i++)
            {
                b = checked(a + b); // when n>46 memory will  overflow
                a = b - a;
            }
            return b;
        }

        private void DoWork_Handler(object sender, DoWorkEventArgs args)
        {
            BackgroundWorker worker= sender as BackgroundWorker;
            for (int i = 1; i < 10; i++)
            {
                if (worker.CancellationPending)
                {
                    args.Cancel = true;
                    break;
                }
                else
                {
                    worker.ReportProgress(i*10);
                    Thread.Sleep(500);
                }
            }
        }

        private void ProcessChanged_Handler(object sender, ProgressChangedEventArgs e)
        {
            progressBar.SetValue(e.ProgressPercentage);
        }

        private void RunWorkerCompleted_Handler(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar.SetValue(0);
            this.progressBar.Close();
        }
    }
}

进度条窗口代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProgressbarTest
{
    public partial class ProgressBar : MaterialSkin.Controls.MaterialForm
    {
        public ProgressBar()
        {
            InitializeComponent();
        }

        public void SetValue(int value)
        {
            this.materialProgressBar1.Value = value;
        }

    }
}

转载于:https://www.cnblogs.com/JackFu/p/7838729.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值