是否需要将两个按钮文本彼此链接?

时间:2019-04-24 15:52:51

标签: c#

我有两个表布局面板,每个面板都有26个按钮。我需要将两个按钮文本相互链接。我有两个列表,其中一种语言用于一组按钮,另一种语言用于另一组按钮。我试图将两个列表链接在一起以设置它们的翻译,但是没有用。因此,例如,如果您单击一个按钮,则将显示文本“ Hello”,而另一个按钮将显示“ Salut”。如果匹配,则它们必须消失。

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);
    }
}

2 个答案:

答案 0 :(得分:0)

我要用字典。我将链接这样的值:

button1.Text == dict[button2.Text]

答案 1 :(得分:0)

好,所以您似乎有两个列表可用于顺序比较。因此,您实际上不需要匹配单词,但是需要匹配两个列表的索引。那讲得通。但是,这一切都令人困惑。

您的代码现在执行以下操作:

  • 创建英语短语列表
  • 创建法语短语列表
  • 开始表格
  • 通过英语面板按钮循环
    • 在控制面板中的按钮上添加随机的英语短语
    • 从英语列表中删除短语
  • 循环浏览法语面板按钮
    • 在控制面板中的按钮上添加随机的法语短语
    • 从法语列表中删除短语
  • 将所有剩余的法语短语添加到剩余的英语短语列表中(无论如何,这一步似乎都是不必要的)

因此,现在您已加载表单。如果面板中的按钮少于列表中的单词,则可能导致两个面板中的单词都无法匹配。

现在您遇到了这个问题:

private void Button_Click2(object sender, EventArgs e)
{
[...]
    if (firstClicked.Text == secondClicked.Text)
    {
        firstClicked = null;
        secondClicked = null;
    }
[...]

这是我看到的唯一尝试比较按钮值的地方。但是,到目前为止,它们将永远无法匹配。例如,“ Hello”是一个永远不会与“ Salut”匹配的字符串。而且,由于列表不再完整,因此不能用来帮助匹配它们。否则,您可以在它们各自的列表中找到短语,然后检查索引是否匹配。

为此,您需要更改在代码中添加按钮文本的方式。不用直接使用列表,而是从列表中创建一个数组。像这样:

    string[] phrases= EngBasicPhrases.ToArray();
    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, phrases.Count - 1);
        button1.Text = phrases[randomNumber];

        phrases.RemoveAt(randomNumber);
    }

    phrases= FrBasicPhrases.ToArray();
    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, phrases.Count);
        button2.Text = phrases[randomNumber];

        phrases.RemoveAt(randomNumber);
    }

在使用变量以及尝试使用颜色确定按钮状态或推断含义时,还有其他一些问题。但是,我会让您解决这些问题。我建议使用断点并逐步执行代码以查看变量的状态并拦截事件。