简体   繁体   中英

c# - Hide button text when another button is clicked

I'm trying to create a matching game that consists of 12 buttons. The program assigns a random string from an array that has 12 strings in it. When the button is pressed, the tag is passed to the button.text. What I'm trying to accomplish now is, for example. If I press "button 1", the text of it changes to "Chevy Camaro". If I press "button 4" next, I want the button1.text to revert back to saying "button 1", rather than it's tag value of "Chevy Camaro". And in like fashion, since "button 4" was pressed, I would like it to show the tag.....

Each button has similar code to it, besides the button #, which of course changes based on the button that's being used.

I'm unsure how to state that, if button is the current active item, then show it's tag property, otherwise, revert back.

private void button4_Click(object sender, EventArgs e)     
{
    button4.Text = button4.Tag.ToString();

    buttoncount++;
    label2.Text = buttoncount.ToString();
}

Thanks in advance for all of your help. Slowly learning this stuff.... =p

You could keep track of the last button that was clicked:

public partial class Form1 : Form
{
    Button lastButton = null;
    int buttoncount;

    public Form1()
    {
        InitializeComponent();
        button1.Tag = "Ford Mustang";
        button2.Tag = "Ford Focus";
        button3.Tag = "Chevy Malibu";
        button4.Tag = "Chevy Camaro";
        button1.Click += button_Click;
        button2.Click += button_Click;
        button3.Click += button_Click;
        button4.Click += button_Click;
        //etc...
    }

    void button_Click(object sender, EventArgs e)
    {
        if (lastButton != null)
        {
            SwitchTagWithText();
        }

        lastButton = sender as Button;
        SwitchTagWithText();

        buttoncount++;
        label2.Text = buttoncount.ToString();
    }

    void SwitchTagWithText()
    {
        string text = lastButton.Text;
        lastButton.Text = lastButton.Tag.ToString();
        lastButton.Tag = text;
    }
}

Could you use a RadioButton control with its appearance set to button? Replace all buttons with these, put them in a GroupBox and the 'reverting' of appearance when clicked off can be automatically handled. To update the text, a simple event handler like below will do;

    private void MakeButton()
    {
        RadioButton rb = new RadioButton
        {
            Appearance = Appearance.Button,
            Tag = "Chevy Camero"
        };
        rb.CheckedChanged += rb_CheckedChanged;
    }

    private void rb_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton clickedButton = sender as RadioButton;
        string currentText = clickedButton.Text;
        clickedButton.Text = clickedButton.Tag.ToString();
        clickedButton.Tag = currentText;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM