gpt4 book ai didi

c# - 在 C# 中单击第三个按钮时使前两个按钮消失

转载 作者:行者123 更新时间:2023-12-04 16:29:49 24 4
gpt4 key购买 nike

下面是我的代码,它工作得很好,除了有一件事不能正常工作。当我点击两个按钮进行匹配时,它们应该保持可见,直到用户点击第三个按钮。我怎样才能做到这一点?谢谢。

namespace Memorija_Seminarska
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
tableLayoutPanel1.Enabled = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
progressBar1.Visible = false;
label2.Text = vreme.ToString();

}

public int vreme = 150;

Random random = new Random();

Button firstClicked = null;
Button secondClicked = null;

List<string> drzava = new List<string>()
{
"Македонија","Македонија", "Бугарија","Бугарија", "Србија","Србија",
"Германија","Германија", "Канада","Канада", "Шпанија","Шпанија",
"Португалија","Португалија", "Австрија","Австрија", "Данска","Данска",
"Индија","Индија", "Италија","Италија", "Англија","Англија",
"Турција","Турција", "Грција","Грција","Хрватска","Хрватска",
"Холандија","Холандија", "Русија", "Русија", "Швајцарија","Швајцарија"
};

private void startButton_Click(object sender, EventArgs e)
{
Add();
tableLayoutPanel1.Enabled = true;
label1.Visible = true;
label2.Visible = true;
progressBar1.Visible = true;
timer2.Start();
timer3.Start();

}

private void Add()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Button b = control as Button;

if (b != null)
{
int randNum = random.Next(drzava.Count);
b.Text = drzava[randNum];
b.ForeColor = b.BackColor;
drzava.RemoveAt(randNum);
}
}
}


private void button_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
return;

Button clickedButton = sender as Button;

if (clickedButton != null)
{
if (clickedButton.ForeColor == Color.Black)
return;


if (firstClicked == null)
{
firstClicked = clickedButton;
firstClicked.ForeColor = Color.Black;
return;

}

secondClicked = clickedButton;
secondClicked.ForeColor = Color.Black;

Win();

if (firstClicked.Text == secondClicked.Text)
{
firstClicked.BackColor = Color.GreenYellow;
secondClicked.BackColor = Color.GreenYellow;

firstClicked = null;
secondClicked = null;
return;

}


timer1.Start();

}
}

private void timer1_Tick(object sender, EventArgs e)
{

timer1.Stop();

firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;

firstClicked = null;
secondClicked = null;

}

private void timer2_Tick(object sender, EventArgs e)
{

vreme--;
label2.Text = vreme.ToString();

if (vreme == 0)
{
tableLayoutPanel1.Enabled = false;

label3.Text = "Game over!";
label3.Visible = true;
label2.Visible = false;
timer2.Stop();
timer3.Stop();
label1.Visible = false;
progressBar1.Visible = false;
}

}

private void timer3_Tick(object sender, EventArgs e)
{
progressBar1.Value -= 1;

if (progressBar1.Value == 0)
{
timer3.Stop();
}
}

private void Win()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Button button1 = control as Button;

if (button1 != null)
{

if (button1.ForeColor == button1.BackColor)
{
return;
}
}
}

label3.Text = "Браво!!!";
label3.Visible = true;
tableLayoutPanel1.Enabled = false;
timer2.Stop();
timer3.Stop();
label2.Visible = false;
progressBar1.Visible = false;
label1.Visible = false;

}



}
}

最佳答案

据我所知,您的 timer1_Tick 处理程序会在时间段到期时自动执行隐藏。如果您希望在单击第三张卡片时手动隐藏按钮,则不应在此处隐藏按钮,而应仅在 button_Click 的开头执行检查:

private void button_Click(object sender, EventArgs e)
{
//two cards are open and not matching (if they matched, they would be already null)
if ( firstClicked != null && secondClicked != null )
{
//hide the buttons
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
}

并从事件处理程序的末尾删除 timer1.Start()

关于c# - 在 C# 中单击第三个按钮时使前两个按钮消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21069561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com