zoukankan      html  css  js  c++  java
  • 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;
            }
    
        }
    }
    
  • 相关阅读:
    Django学习之路
    Bootstrap插件及其应用方法网址
    BOM和DOM
    jQuery学习
    初识JS之数据类型
    前端学习之路CSS基础学习二
    前端学习之路CSS基础学习一
    前端学习之路
    ORM
    Python Twisted 学习系列4(转载stulife最棒的Twisted入门教程)
  • 原文地址:https://www.cnblogs.com/JackFu/p/7838729.html
Copyright © 2011-2022 走看看