2

我想要一个进度条,如果按下按钮(MouseDown 事件),进度条的值会上升,进度条需要同时上升。

有任何想法吗?我试过用计时器,但这就是我目前所拥有的

private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.PerformStep();
            progressBar1.UseWaitCursor = true;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
        }
        public void button1_MouseDown(object sender, MouseEventArgs e)
        {
            timer1.Start();

        }
        public void button1_MouseUp(object sender, MouseEventArgs e)
        {
            timer1.Stop();  
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

        }
4

2 回答 2

1

它应该如下所示:

void timer_Tick(object sender, EventArgs e) {
  progressBar1.PerformStep();      
}

void button1_MouseDown(object sender, MouseEventArgs e) {
  timer.Start();
}

void button1_MouseUp(object sender, MouseEventArgs e) {
  timer.Stop();
}
于 2013-11-04T17:14:30.707 回答
1

以下程序将每 20 毫秒更新一次进度条,并在 2 秒后获得 10 100%

public partial class Form1 : Form
{

    private Timer pbTimer;
    private int pbProgress = 0;

    public Form1()
    {
        InitializeComponent();
        pbTimer = new Timer();
        pbTimer.Tick += new EventHandler(ProgressUpdate);
        pbTimer.Interval = 20;
        this.MouseDown += Form1_MouseDown;
        this.MouseUp += Form1_MouseUp;
    }

    private void ProgressUpdate(object sender, EventArgs e)
    {
        if (pbProgress < 100)
        {
            progressBar1.Value = ++pbProgress;
        }
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        pbTimer.Start();
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        pbTimer.Stop();
        progressBar1.Value = 0;
        pbProgress = 0;
    }
于 2013-11-04T17:24:15.970 回答