为完整进度条编写 If 语句

Writing An If Statement For A Full Progress Bar

基本上它是一个静态登录表单。我创建了一个进度条 (progressBar1) 并在 2 个文本框中输入文本(一个用于 ID,一个用于密码)。 ID 和密码是硬编码的,即 id="admin" 和 password="admin"。我想要的是,当我按下一个按钮(button1)时,进度条应该在两种情况下启动 5 秒:如果输入的 ID 和密码正确,那么当进度条达到最大值时它应该显示另一种形式,否则 messageBOX应该说进度条达到最大长度后输入的信息不正确

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

现在请帮助我如何编写该代码,因为我已经浪费了 10 个小时来尝试。

您可以使用计时器来增加进度条。 假设您的表单构造函数称为 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();
        }
}

然后从 button1 的点击事件中调用

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

我还没有在编译器上测试过这段代码,但它应该能让你知道该怎么做