简体   繁体   English

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

[英]How to show message when last command on button clicked?

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

at the moment when I click "Next" button it reaches "year 10" thereafter shows error message (An unhandled exception of type 'System.IndexOutOfRangeException' occurred in WindowsFormsApplication1.exe). 当我单击“下一步”按钮时,它达到“第10年”,此后显示错误消息(WindowsFormsApplication1.exe中发生了类型为'System.IndexOutOfRangeException'的未处理异常)。

I want to be able to click the Next button after year 10 and it show a message eg year 10 complete, end of game etc. Any help would be appreciated on how to achieve this. 我希望能够在10年后单击“下一步”按钮,并且它显示一条消息,例如10年完成,游戏结束等。在实现这一目标方面将不胜感激。

We strongly recommend you to cover at least the C# basics and read some tutorials about it. 我们强烈建议您至少涵盖C#基础知识并阅读一些有关它的教程。

Sample: 样品:

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

or visa versa 或反之亦然

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM