c# - Need to link two button texts to eachother?
问题描述
I have two table layout panels with 26 buttons each. I need to link the two button texts to each other. I have two Lists containing one language for one set of buttons and another language for another set of buttons. I tried to link the two lists together in order to set their translations but it didnt work. So for example if you click on one button the text "Hello" will show and another button "Salut". If its a match they must either disappear.
Random random = new Random();
List<string> EngBasicPhrases = new List<string>()
{
"Hello", "How are you?", "Hot", "Thank you", "Welcome",
"Let's go", "My name is...", "Cold", "Good luck",
"Congratulations", "Bless you","I forgot","Sorry","I'm fine",
"It's no problem","Don't worry","Here it is","What?","Of course",
"Boy","Girl","Man","Woman","Friend","Almost","Late"
};
List<string> FrBasicPhrases = new List<string>()
{
"Salut","Ca va?","Chaud", "Merci", "Bienvenu", "Allons-y","Je m'appelle","Du froid",
"Bonne chance","Felicitations","A vos souhaits","J'ai oublie","Desole","Je vais bien",
"Ce n'est pas grave","Ne t'en fais pas","Voila","Comment?","Bien sur","Un garcon","Une fille",
"Un home","Une femme","Un ami","Presque","En retard"
};
Button firstClicked, secondClicked;
public Game()
{
InitializeComponent();
AssignWordsToSquares();
EngBasicPhrases.AddRange(FrBasicPhrases);
}
private void Button_Click(object sender, EventArgs e)
{
if (firstClicked != null && secondClicked != null)
return;
Button clickedButton = sender as Button;
if (clickedButton == null)
return;
if (clickedButton.ForeColor == Color.Black)
return;
if(firstClicked == null)
{
firstClicked = clickedButton;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedButton;
secondClicked.ForeColor = Color.Black;
CheckForWinner1();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
else
timer1.Start();
}
private void CheckForWinner1()
{
Button button1;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
button1 = tableLayoutPanel1.Controls[i] as Button;
if (button1 != null && button1.ForeColor == button1.BackColor)
return;
}
MessageBox.Show("Congratulations!");
}
private void Button_Click2(object sender, EventArgs e)
{
if (firstClicked != null && secondClicked != null)
return;
Button clickedButton = sender as Button;
if (clickedButton == null)
return;
if (clickedButton.ForeColor == Color.Black)
return;
if (firstClicked == null)
{
firstClicked = clickedButton;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedButton;
secondClicked.ForeColor = Color.Black;
CheckForWinner2();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
else
timer1.Start();
}
private void CheckForWinner2()
{
Button button2;
for (int i = 0; i < tableLayoutPanel2.Controls.Count; i++)
{
button2 = tableLayoutPanel2.Controls[i] as Button;
if (button2 != null && button2.ForeColor == button2.BackColor)
return;
}
MessageBox.Show("Congratulations!");
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
private void AssignWordsToSquares()
{
Button button1 = button2;
int randomNumber;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i] is Button)
button1 = (Button)tableLayoutPanel1.Controls[i];
else
continue;
randomNumber = random.Next(0, EngBasicPhrases.Count);
button1.Text = EngBasicPhrases[randomNumber];
EngBasicPhrases.RemoveAt(randomNumber);
}
for (int i = 0; i < tableLayoutPanel2.Controls.Count; i++)
{
if (tableLayoutPanel2.Controls[i] is Button)
button2 = (Button)tableLayoutPanel2.Controls[i];
else
continue;
randomNumber = random.Next(0, FrBasicPhrases.Count);
button2.Text = FrBasicPhrases[randomNumber];
FrBasicPhrases.RemoveAt(randomNumber);
}
}
解决方案
我会使用字典。我会像这样链接值:
button1.Text == dict[button2.Text]
推荐阅读
- javascript - Google Firebase uid 网络
- r - 关于 R 中的 factoextra 包的问题
- mysql - 将带有“+”分隔符的字符串拆分为单独的行并应用聚合
- java - 当传入流量使用负载均衡器 URL 时,如何捕获用于满足请求的服务器?
- python - 当我尝试将图像数据和标签插入到我的预训练张量流模型(shuffleNet)中时出现关键错误
- sql - Laravel Eloquent 在日期上带有 `has` 和 `where`
- asp.net-mvc - Angular 生产版本不会在 dist 中创建 my-app 文件夹 - Azure DevOps
- javascript - React js:导入组件到路由
- java - 在 Linux VM (Red hat 6.5) 中为 jvisualvm 显示 env var
- kubernetes - Kubernetes 上的 Keycloak 集群生产设置 - Google K8S Engine (GKE)