【问题标题】:How to show message when last command on button clicked?单击按钮上的最后一个命令时如何显示消息?
【发布时间】:2016-11-03 17:55:45
【问题描述】:
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 中发生了“System.IndexOutOfRangeException”类型的未处理异常)。

我希望能够在第 10 年之后单击“下一步”按钮,它会显示一条消息,例如第 10 年完成、游戏结束等。任何有关如何实现此目的的帮助将不胜感激。

【问题讨论】:

  • 你听说过if这句话吗?
  • 命中:使用if 比较yyear.Length

标签: c# button messagebox


【解决方案1】:

我们强烈建议您至少了解 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;
}

【讨论】:

  • 这让我想删除我的答案。不错哈哈
猜你喜欢
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多