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

本文关键字:命令 最后的 单击 按钮 何显示 显示 消息 | 更新日期: 2023-09-27 18:16:16

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年",此后显示错误消息(类型为"系统"的未处理异常)。在WindowsFormsApplication1.exe中发生IndexOutOfRangeException。

我希望能够在第10年之后单击Next按钮,它显示一个消息,例如第10年完成,游戏结束等。关于如何实现这一目标,任何帮助都将不胜感激。

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

我们强烈建议你至少学习c#基础知识,并阅读一些相关教程。

  • if-else语句文档

  • c# operator文档

示例:

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;
}