单击按钮上的最后一个命令时如何显示消息?

时间:2016-11-03 17:55:46

标签: c# button messagebox

public partial class Form1 : Form
{
    string[] year = { "Year 1", "Year 2", "Year 3", "Year 4", "Year 5", "Year 6", "Year 7", "Year 8", "Year 9", "Year 10" };
    static int y = 0;
    public Form1()
    {
        InitializeComponent();
    }
    // the code for the progressBar in nextButton_Click was taken from http://www.codeproject.com/Articles/745453/Visual-Basic-Progress-Bar-control
    private void nextButton_Click(object sender, EventArgs e)
    {
        progressBar1.PerformStep();
        yearLabel.Text = year[y];
        y = y + 1;
    }
}

当我点击"下一步"按钮它到达" 10年级"此后显示错误消息(类型' System.IndexOutOfRangeException'在WindowsFormsApplication1.exe中发生的未处理异常)。

我希望能够在第10年后点击“下一步”按钮,它会显示一条消息,例如10年完成,游戏结束等。如果能够实现此目的,我们将不胜感激。

1 个答案:

答案 0 :(得分:2)

我们强烈建议您至少涵盖C#基础知识,并阅读一些有关它的教程。

示例:

if (y != year.Length)        
{       
    progressBar1.PerformStep();
    yearLabel.Text = year[y];
    y = y + 1;               
}
else
{
    MessageBox.Show("year 10 complete");
}

或反之亦然

if (y == year.Length)            
{
    MessageBox.Show("year 10 complete");          
}
else
{
    progressBar1.PerformStep();
    yearLabel.Text = year[y];
    y = y + 1;
}