开发者

use of progress bar in winforms

i simply want that the progress bar progresses a single step , as the timer ticks up one second, but couldn't do it. please help.

shall i use a variable , i , and increment i by 1, in the tick event of the timer. and write : progressBar1.Increment(i) --i tried this and it worked.

but why isn't it working with the following code:

public partial class Form1 : Form
{
    Timer t = new Timer();

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs 开发者_运维百科e)
    {
        t.Interval = 1000;
        t.Enabled = true;
        t.Tick += new EventHandler(t_Tick);
    }
    void t_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(1);
    }

as one second passes, the tick event occurs, and the progressBar should increment by 1, but here, its just stuck up only at a single increment, i.e. it progresses only by 1 and stops.


It should work. Try enabling the timer after assigning the event handler:

private void Form1_Load(object sender, EventArgs e)
{
    t.Interval = 1000;
    t.Tick += new EventHandler(t_Tick);
    t.Enabled = true;
}


Your code is probably working. The problem is your maximum value on your progress bar is probably so high that it takes a while for the value of the progress bar to be high enough to display the next block.

Set your maximum to 100 and you should see that your code works fine.


Missing Timer Start, below code suppose to work.

public partial class Form1 : Form
{
    Timer t = new Timer();

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        t.Interval = 1000;
        t.Tick += new EventHandler(t_Tick);
        // enable timer after the handler attached
        t.Enabled = true;
        // Start the timer.
        t.Start();
    }
    void t_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(1);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜