MouseDown事件和进度条

时间:2013-11-04 16:58:13

标签: c# .net winforms progress-bar mousedown

我想有一个进度条,如果按下按钮(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)
        {

        }

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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;
    }