如何将标签文本与按钮文本值进行匹配 [英] How to match the label text to the button text value

查看:76
本文介绍了如何将标签文本与按钮文本值进行匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c#编程的新手,并在Windows窗体中创建一个简单的rolldice游戏。我的表单由两个图片框组成,mysumLabel标签,rolldice按钮和Tablelayoutpanel,由9个标记为1-9的按钮组成。游戏模拟两个六面骰子的滚动。单击rolldice按钮时,骰子面部图像随机显示在两个图片框中,其面部数量的总和将显示在mysumLabel标签上。根据mysum标签的值,玩家必须通过点击它来为Tablelayoutpanel中的相应按钮着色。如果玩家获得连续,列或对角线上的三个按钮,则玩家获胜。获胜的功能正常工作我的问题是我只想启用对应于mysumLabel标签值的按钮,应该禁用Tablelayoutpanel中的其余按钮。类似下面的伪代码:



I am a newbie to c# programming and creating a simple rolldice game in windows forms. My form consists of two pictureboxes, mysumLabel label, rolldice button and Tablelayoutpanel consisting of 9 buttons labelled 1-9. The game simulates the rolling of two six sided dice. When clicking the rolldice button the dice face images get randomly displayed in the two pictureboxes and the sum of their number of faces get displayed on the mysumLabel label. Based on the value of mysum label, the player has to color the corresponding button in the Tablelayoutpanel by clicking on it. The player wins if he gets three buttons colored in a row, column or diagonally. The functionality for winning is working fine my problem is that I only want the button corresponding to the value of mysumLabel label to be enabled, the rest of buttons in the Tablelayoutpanel should be disabled. Something like pseudocode below:

If(mysumLabel.Text= myButton1.Text)
{
  myButton2.Enabled= myButton3.Enabled= myButton3.Enabled= myButton4.Enabled=
  myButton5.Enabled= myButton6.Enabled= myButton7.Enabled= myButton8.Enabled=
  myButton9.Enabled=false
}
else
{

}

推荐答案

您好,dihawa您可以将按钮添加到Button数组,然后您可以根据需要启用和禁用按钮。这是一个示例代码匹配您的需求。希望这对你有所帮助:-)



Hi,dihawa you can add your buttons to an Button array and then you can enable and disable buttons as you want.Here is a sample code matches your requirement. Hope this helps to you :-)

Button[] arrbtn = new Button[4];
arrbtn[0] = myButton1;
arrbtn[1] = myButton2;
arrbtn[2] = myButton3;
arrbtn[3] = myButton4;

  foreach(Button k in arrbtn)
     {
       if (mysumLabel.Text.Equals(k.Name))
           {
              k.Enabled = true;
           }
       else
           {
              k.Enabled = false;
           }
     }


这篇关于如何将标签文本与按钮文本值进行匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆