• 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;
            }
    
        }
    }
    
  • 相关阅读:
    InnoDB实现MVCC原理
    Python中定义函数时参数有默认值的小陷阱
    Python系统编程笔记
    Python中的字典
    Python中常见的字符串小笔试题
    Oracle常见名词解析
    Oracle数据库面试题【转载】
    Oracle日期语言修改
    Oracle日期时间函数大全
    Oracle数据库分页的三种方法
  • 原文地址:https://www.cnblogs.com/JackFu/p/7838729.html
Copyright © 2020-2023  润新知