每秒显示不同的标签文本

时间:2014-10-03 20:23:28

标签: c# visual-studio-2010 visual-studio timer label

我有三种形式,从1开始是主窗体,Form2是初始屏幕,Form3是迷你窗体。 所以我想要做的就是当我发送邮件时,这个From3会弹出一个有gif图像 和一个标签。所以我成功地使From3持续5秒,其中标签将 每秒更改不同的words

表格3 我使用timer1只运行迷你表单5秒钟。和计时器2只运行1秒。

我想我们可以用更好的方式做到这一点这非常简单容易..任何好主意和 非常欢迎帮助!!!

注意: - 当我再次按下按钮发送邮件..标签从 - Done!!开始..任何帮助..第一次从Connecting to smtp server..开始,但第二次开始它从Done!!开始,然后标签转到Connecting to smtp server..等等!! 这是我的代码:

Form1中

 private void sendmail_Click(object sender, EventArgs e)
    {
     //mail basic function here!!!!
     smtp.Send(msg);
     _f3.ShowDialog();//- - ->> goes to mini form Form3
     smtp.Dispose();
     MessageBox.Show("Email Successfully Sent!!!", "Mail!!!.");
     }

Form3

 private void Form3_Load(object sender, EventArgs e)
{
    timer1.Interval = 5000;
    timer1.Enabled = true;
    timer1.Tick += new EventHandler(timer1_Tick);

    timer2.Interval = 1000;
    timer2.Start();
}


private void timer1_Tick(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
    timer1.Stop();
}

int StopTime = 0;
private void timer2_Tick(object sender, EventArgs e)
{
    StopTime++;
    if (StopTime == 1)
    {
        label1.Text = "  Connecting to smtp server..";
    }
    if (StopTime == 2)
    {
        label1.Text = "     Fetching recipients..";
    }
    if (StopTime == 3)
    {
        label1.Text = "  Attaching G-code files..";
    }
    if (StopTime == 4)
    {
        label1.Text = "                Done!!";
        StopTime = 0;
        timer2.Stop();
    }
}

1 个答案:

答案 0 :(得分:2)

Form3显示完成后是否关闭?你可以这样做:

    private void Form3_Load(object sender, EventArgs e)
    {
        SetLabel1Text(); //reset label text
        timer1.Interval = 5000;
        timer1.Enabled = true;
        timer1.Tick += new EventHandler(timer1_Tick);

        timer2.Interval = 1000;
        timer2.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        timer1.Stop();
    }

    int StopTime = 0;
    private void timer2_Tick(object sender, EventArgs e)
    {
        StopTime++;
        SetLabel1Text();
        if (StopTime == 4)
        {
            StopTime = 0;
            timer2.Stop();
        }
    }

    private void SetLabel1Text()
    {
        string[] label1Text = { "  Connecting to smtp server..", "  Connecting to smtp server..", "     Fetching recipients..", "  Attaching G-code files..", "                Done!!" };
        label1.Text = label1Text[StopTime]; //populate label from array of values
    }