【问题标题】:How to create an if-statement to check if a button text has changed to a specific value c#如何创建一个 if 语句来检查按钮文本是否已更改为特定值 c#
【发布时间】:2020-04-26 12:32:00
【问题描述】:

我目前正在为一个学校项目开发一个小程序,但似乎根本找不到解决方案。

我一直在处理带有显示文本标签的 Windows 窗体。然后是两个按钮。如果您单击了这些按钮中的任何一个,则另一个将被隐藏,并且已单击按钮的文本会发生变化。

现在我想检查这些按钮的文本是否已更改,然后再次单击现在更改的按钮关闭应用程序。以下是我目前所拥有的。

    private void Form1_Load(object sender, EventArgs e)
    {
        label1.Text = "You wake up.\n\n"+
                      "What would you like to do?";

        button1.Text = "I'd like to eat some cerials!";
        button2.Text = "I need to go to the bathroom!";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = "You stand up, go to the kitchen and eat some cerials. Nice!";
        button1.Text = "Hurray, you ate some cerials";
        button2.Hide();

        // if (button1.TextChanged +=)
        // {
        //     
        // }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        label1.Text = "You don't finde the Bathroom in this house and have to use the garden.";
        button2.Text = "You get arested afterwars!";
        button1.Hide();
    }

    public void EndGame()
    {
        Close();
    }

有没有办法真正做到这一点,还是我需要一些解决方法?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    编辑:

    • 创建新的网络表单应用程序
    • 在 Form1 button1,label1,textbox1 上创建
    • 双击表单添加LoadEvent
    • 双击按钮添加点击事件

      public Form1()
      {
          InitializeComponent();
      }
      
      //copy from here
      public class AppState
      {
          public string labelText { get; set; }
          public bool endGame { get; set; }
          public bool success { get; set; }
          public List<AppState> answers { get; set; }
      }
      
      AppState level = new AppState()
      {
          labelText = "You are ... you can go 1.left 2.right 3.further ",
          answers = new List<AppState>{
             new AppState() {
              labelText = "You are dead",
              endGame = true,
             },
             new AppState() {
              labelText = "You are winner",
              endGame = true,
              success = true
             },
             new AppState() {
              labelText = "You are bla bla.. 1. left 2. right",
              answers = new List<AppState>{
                 new AppState() {
                  labelText = "You are dead",
                  endGame = true,
                 },
                 new AppState() {
                  labelText = "You are winner",
                  endGame = true,
                  success = true
                 }
              }
             }
            }
      };
      
      public void Render()
      {
          label1.Text = level.labelText;
          if (level.endGame)
          {
              button1.Enabled = false;
              if (level.success)
              {
                  MessageBox.Show("winner");
              }
              else
              {
                  MessageBox.Show("looser");
              }
          }
      }
      
      public void Answer()
      {
          int ans = int.Parse(textBox1.Text);
          level = level.answers[ans - 1];
          Render();
      }
      // copy to here        
      
      //this you need to do manualy with form editor
      private void Form1_Load(object sender, EventArgs e)
      {
          Render();
      }
      
      //this you need to do manualy with form editor
      private void button1_Click(object sender, EventArgs e)
      {
          Answer();    
      }
      

    【讨论】:

    • 很好,伙计。之后我会确保检查出来
    • 创建了一个新类并 100% 复制了您的代码。但它仍然无法正常工作。
    • 我遇到了各种各样的错误,伙计。但这并不意味着它与您的代码有关,更多的是与我的技能有关。
    【解决方案2】:

    有几种方法可以轻松做到这一点。一种方法是在单击按钮时设置一个全局布尔值。如果您的布尔值命名为 boolean endProgram = false,那么您的 if 语句将如下所示:

    if(endProgram)
    {
        EndGame()
    }
    

    万一你不知道,if(endProgram)if(endProgram == true) 是一样的。这仅适用于布尔值。

    只需将相同的内容放入另一个按钮单击事件中,就可以了。

    【讨论】:

    • 谢谢伙计。现在不在我的电脑上,但我会确保尝试一下!我会告诉你它是否有效。
    • 不确定我这样做是否正确。我添加了布尔值并将 if 语句添加到 button1_Click 和 button2_Click 但它似乎根本没有做任何事情。也许我做错了?
    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 2014-11-27
    • 2021-07-06
    • 2020-02-06
    • 2020-04-27
    • 2021-10-25
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多