문제

I would like to have a progress bar, where the value of the bar would rise if the button was being pressed down(MouseDown Event), the bar needs to rise simultaneousely.

Any ideas? I tried with a timer but this is all I have currently

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)
        {

        }
도움이 되었습니까?

해결책

It should look like this:

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();
}

다른 팁

The following program will update the progress bar each 20 ms the mouse is held down and get 10 100% after 2 seconds

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;
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top