C#如果lastbutton标记等于当前单击的按钮,则禁用这两个标记
我正在创建一个匹配的游戏。我试图完成的是一项检查,检查之前单击的按钮的标记是否与“当前”单击的按钮的标记匹配。如果这些标签匹配,它将禁用这两个按钮,因为它们不再是游戏中的选项。 我困惑的一部分是,在哪里集成这部分代码而不破坏我的大部分工作C#如果lastbutton标记等于当前单击的按钮,则禁用这两个标记,c#,button,C#,Button,我正在创建一个匹配的游戏。我试图完成的是一项检查,检查之前单击的按钮的标记是否与“当前”单击的按钮的标记匹配。如果这些标签匹配,它将禁用这两个按钮,因为它们不再是游戏中的选项。 我困惑的一部分是,在哪里集成这部分代码而不破坏我的大部分工作 Random myRandom = new Random(); var buttons = new List<Button> { button1, button2, button3, button4, button5, button6, b
Random myRandom = new Random();
var buttons = new List<Button> { button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12 };
var carString = new List<string> { "Camaro", "Mini Cooper", "Porsche 944", "Ford Focus", "Chevy Blazer", "Model T", "Camaro", "Mini Cooper", "Porsche 944", "Ford Focus", "Chevy Blazer", "Model T" };
while (matchingButtonIndex < numOfButtons)
{
int index = myRandom.Next(carString.Count);
var name = carString[index];
if (name != null)
{
buttons[matchingButtonIndex].Tag = name;
carString[index] = null;
matchingButtonIndex = matchingButtonIndex + 1;
}
}
}
void SwitchTagWithText()
{
string text = lastButton.Text;
lastButton.Text = lastButton.Tag.ToString();
lastButton.Tag = text;
}
private void button1_Click(object sender, EventArgs e)
{
if (lastButton != null)
{
SwitchTagWithText();
}
lastButton = sender as Button;
SwitchTagWithText();
buttoncount++;
label2.Text = buttoncount.ToString();
}
Random myRandom=new Random();
变量按钮=新列表{button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11,button12};
var carString=新列表{“Camaro”、“Mini Cooper”、“Porsche 944”、“福特福克斯”、“雪佛兰运动夹克”、“T型”、“Camaro”、“Mini Cooper”、“Porsche 944”、“福特福克斯”、“雪佛兰运动夹克”、“T型”};
while(匹配按钮索引
我建议您创建一个新类来处理此问题,例如
public class ButtonManager {
private Button lastButton;
public void SwitchTagWithText(Button button){
string text = lastButton.Text;
lastButton.Text = lastButton.Tag.ToString();
lastButton.Tag = text;
// Or whatever the logic is you need.
}
public void ButtonClicked(Button button){
this.SwitchTagWithText(button);
// Or whatever the logic is you need.
}
}
好的,上面的内容是不完整的,但是希望主要的想法能够实现——您有一个单独的类来处理标记值的读取/设置,并确定是否应该禁用按钮
因此,在from load中,创建一个可在表单代码中使用的新实例
ButtonManager
,将其设置为一个字段。您有一个按钮计数变量,每次单击都会增加该变量。所以你可以用它:
private void button1_Click(object sender, EventArgs e)
{
thisButton = sender as Button;
buttoncount++;
SwitchTagWithText(thisButton);
if (buttoncount == 1)
{
lastButton = thisButton;
}
else if (buttoncount == 2)
{
if (lastButton.Tag == thisButton.tag)
{
// Disable both buttons.
}
else
{
// Switch buttons back
}
buttoncount = 0;
}
}
比如:
private void button1_Click(object sender, EventArgs e)
{
if (lastButton != null)
{
SwitchTagWithText();
var thisButton = sender as Button;
if(thisButton.Text != lastButton.Text
&& thisButton.Tag.ToString() == lastButton.Tag.ToString())
{
//two different buttons with the same tag were clicked in succession
thisButton.Enabled = false;
lastButton.Enabled = false;
lastButton = null;
return;
}
lastButton = thisButton;
}
else
{
lastButton = sender as Button;
}
SwitchTagWithText();
buttoncount++;
label2.Text = buttoncount.ToString();
}
我认为您的第一个方法中省略了方法声明。