winForm进度条及进度信息提示

来源:互联网 发布:linux deploy镜像站 编辑:程序博客网 时间:2024/06/11 21:46

主窗体:


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

namespace ShowWaiting
{
    public partial class frmTest : Form
    {
        public frmTest()
        {
            InitializeComponent();
        }
        //创建代理。
        private ProgressbarForm myProcessBar = null;//弹出的子窗体(用于显示进度条)
        private delegate bool IncreaseHandle(int nValue, string vinfo);//代理创建
        private IncreaseHandle myIncrease = null;//声明代理,用于后面的实例化代里
        private int vMax = 1000;//用于实例化进度条,可以根据自己的需要,自己改变
        private void button2_Click(object sender, EventArgs e)
        {
            Thread thdSub = new Thread(new ThreadStart(ThreadFun));
            thdSub.Start();
        }
        private void ThreadFun()
        {
            MethodInvoker mi = new MethodInvoker(ShowProcessBar);
            this.BeginInvoke(mi);
            Thread.Sleep(100);
            object objReturn = null;
            for (int i = 0; i < vMax; i++)
            {
                objReturn = this.Invoke(this.myIncrease, new object[] { 1, i.ToString() + "\r\n" });
                Thread.Sleep(50);
            }
        }
        private void ShowProcessBar()
        {
            myProcessBar = new ProgressbarForm (vMax);
            myIncrease = new IncreaseHandle(myProcessBar.Increase);
            myProcessBar.ShowDialog();
            myProcessBar = null;
        }
    }
}

子窗体:进度条弹框


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;


namespace ShowWaiting
{
    /// <summary>
    /// <para>[功能描述]:进度条</para>
    /// <para>[  类名称]:AutoTestSet.ProgressbarForm</para>
    /// <para>[版权信息]:Copyright (c) 1995-2015 TCB Corporation</para>
    /// <para>[日期时间]:2016/8/12 10:03</para>
    /// </summary>
    public partial class ProgressbarForm : Form
    {
        public ProgressbarForm(int vMax)
        {
            InitializeComponent();
            this.progressBar1.Maximum = vMax;
        }


        public bool Increase(int nValue, string nInfo)
        {
            if (nValue > 0)
            {
                if (progressBar1.Value + nValue < progressBar1.Maximum)
                {
                    progressBar1.Value += nValue;
                    this.labelControl2.Text = nInfo;
                    Application.DoEvents();
                    progressBar1.Update();
                    progressBar1.Refresh();
                    this.labelControl2.Update();
                    this.labelControl2.Refresh();
                    return true;
                }
                else
                {
                    progressBar1.Value = progressBar1.Maximum;
                    this.labelControl2.Text = nInfo;
                    this.Close();//执行完之后,自动关闭子窗体
                    return false;
                }
            }
            return false;
        }
    }
}

来自:http://blog.163.com/china__xuhua/blog/static/199723169201231735439614/

0 0
原创粉丝点击