0
votes

Basically its a static login form. I have created a progress bar (progressBar1) and I enter text in 2 text boxes(one for id & one for password). ID and password are hard coded i.e id="admin" and password="admin". The thing I want is that when I press a button (button1), progress bar should start for 5 sec for both cases that are: if entered ID and password are correct then it should show another form when progress bar reaches to maximum and else messageBOX should say that entered info is not correct after progress bar reaches to maximum length

private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "admin" && textBox2.Text == "admin")
        {
            form2 f2 = new form();
            this.Hide();
            f2.Show();
        }
    }

Now please help me how can i do that code as I have wasted 10 hours on trying.

1
Just to clarify, if password and username is equal to admin, you want to display a progress bar that goes from 0 to 100% over the course of 5 seconds, after that you want to display another form? - Rasmus Søborg
yup @dotTutorials u have got my point :) - Hameer Siddique
Use Timer. Period. - Nikhil Vartak
@NikhilVartak can u plz tell me this with code? I will be thankful to u. - Hameer Siddique
Not until you try to use it and show your efforts first. That's how SO works. - Nikhil Vartak

1 Answers

1
votes

You could use a Timer to increment your progressbar. Assuming your the constructor to your form is called Form1.

private Timer m_Timer;

private Form1() { // constructor
    m_Timer = new Timer(500); // updates progressbar every 500 ms
    progressBar1.Maximum = 5000; // MaxValue is reached after 5000ms
    m_Timer.Elapsed += async (s, e) => await mTimerTick();
}

private async Task mTimerTick() {
        progressBar1.Value += m_Timer.Interval;
        if (progressBar1.Value >= progressBar1.Maximum) {
            m_Timer.Stop();
            this.Hide();
            var f2 = new Form();
            f2.Show();
        }
}

And from your button1's click event you would call

private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "admin" && textBox2.Text == "admin")
        {
            m_Timer.Start();
        }
    }

I haven't tested this code on a compiler, but it should give you an good idea of what to do