WinForm简单进度条

 

无标题

 

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    int current = 0;
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        progressBar1.Maximum = (int)numericUpDown1.Value;
        for (int i = 0; i < progressBar1.Maximum; i++)
        {
            System.Threading.Thread.Sleep(10);
            backgroundWorker1.ReportProgress(++current);
        }
        //MessageBox.Show(current.ToString());   
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
 
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
        try { int.Parse(numericUpDown1.Text); }
        catch
        {
            MessageBox.Show("工作量必须为数字!");
            return;
        }
        if (backgroundWorker1.IsBusy)
        {
            MessageBox.Show("已经在处理中,请稍后!");
            return;
        }
 
        backgroundWorker1.RunWorkerAsync();
    }
 
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
 
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressBar1.Value = 0;
        MessageBox.Show("OK<");
    }
}
posted @ 2010-09-16 19:11  MHL  阅读(366)  评论(0编辑  收藏  举报