C# 进度条

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

namespace ProgressBar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.progressBar1.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.progressBar1.Visible = true;
            this.llabelProcess.Visible = true;
            // 启动进度条线程
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(NumberIncrement));
            thread.Start();
            Application.DoEvents();
            

        }
        
        public void NumberIncrement()
        {
             EventHandler eh = new EventHandler(DisplayNumber(), pList);//启用一个委托,跳回UI线程.
             try
             {
                 this.Invoke(eh, new object[] { null, null });//执行委托
             }
             catch (System.Exception ex)
             {
 
             }
        }

        public void DisplayNumber(object sender, EventArgs e)
        {
            for (int i = 1; i <= 100; i++)
            {
                SetProcessValue("测试中...", i);
                Thread.Sleep(50);
            }
        }

        //设置进度条显示
        public void SetProcessValue(string TitleName, int value)
        {
            this.progressBar1.Tag = TitleName;
            string str = value.ToString() + "%";
            Font font = new Font("Times New Roman", (float)11, FontStyle.Regular);
            PointF pt = new PointF(this.progressBar1.Width / 2 - 10, this.progressBar1.Height / 2 - 10);
            this.progressBar1.Value = value;
            this.llabelProcess.Text = str;
            this.Refresh();

            if (value >= 100)
            {
                MessageBox.Show("导出完成,请保存!");
                this.button1.Enabled = true;
                this.progressBar1.Visible = false;
                this.llabelProcess.Visible = false;
            }
        }
    }
}

 

更多内容请访问 www.uusystem.com

posted @ 2018-07-19 10:01  uusystem  阅读(1918)  评论(0编辑  收藏  举报