简体   繁体   English

c#-单击另一个按钮时隐藏按钮文本

[英]c# - Hide button text when another button is clicked

I'm trying to create a matching game that consists of 12 buttons. 我正在尝试创建一个包含12个按钮的匹配游戏。 The program assigns a random string from an array that has 12 strings in it. 该程序从其中包含12个字符串的数组中分配一个随机字符串。 When the button is pressed, the tag is passed to the button.text. 当按下按钮时,标签将传递到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". 如果按“按钮1”,则其文本将更改为“ 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". 如果我接下来按下“按钮4”,则我希望button1.text恢复为说“按钮1”,而不是“ Chevy Camaro”的标签值。 And in like fashion, since "button 4" was pressed, I would like it to show the tag..... 而且,由于按下了“按钮4”,我希望它显示标签。

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. 我不确定如何声明,如果button是当前活动项,则显示其tag属性,否则,返回原状态。

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? 您可以使用外观设置为button的RadioButton控件吗? Replace all buttons with these, put them in a GroupBox and the 'reverting' of appearance when clicked off can be automatically handled. 用这些按钮替换所有按钮,将它们放在GroupBox中,单击时可以自动处理外观的“还原”。 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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