首页 > 解决方案 > 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);
    }
}

标签: c#

解决方案


我会使用字典。我会像这样链接值:

button1.Text == dict[button2.Text]

推荐阅读