开发者

How would you code many buttons into one Method?

开发者 https://www.devze.com 2023-01-30 14:23 出处:网络
I\'m trying to fix a computer program in C# for the game MasterMind. Right now, there is a lot of unnecessary code for the buttons. I know i can put them all into one method but i don\'t know how. Her

I'm trying to fix a computer program in C# for the game MasterMind. Right now, there is a lot of unnecessary code for the buttons. I know i can put them all into one method but i don't know how. Here is some of the code. Please help.

private void button1_Click(object sender, EventArgs e)
        {
            this.ActiveControl.BackColor = controlColor;
            this.ActiveControl.Text = controlNumber;
            allCellsClicked[0] = '1';
            if (all_Buttons_Clicked())
            {
                allCellsClicked[0] = '0';
                allCellsClicked[1] = '0';
                allCellsClicked[2] = '0';
                allCellsClicked[3] = '0';
                button04.Enabled = false;
                button03.Enabled = false;
                button02.Enabled = false;
                button01.Enabled = false;
                guess++;
     开发者_如何学编程           Label1.Text = "Guess Number " + Convert.ToString(guess);
                label4.Visible = true;
                label5.Visible = true;
                label4.Text = "0";
                label5.Text = "0";
int a = int.Parse(button01.Text), b = int.Parse(button02.Text), c = int.Parse(button03.Text), d = int.Parse(button04.Text);
int rightCol, rightPos;
                CheckAnswer(a, b, c, d, out rightPos, out rightCol);
                label4.Text = rightCol.ToString();
                label5.Text = rightPos.ToString();


If what you are saying is that all of the buttons execute essentially the same code but it is copied multiple times, then you can look to the place where each button's click event is being hooked up to each of their event handlers and point them all to the same method.

Somewhere (probably in your filename.Designer.cs), you have something like this:

button1.Click += new EventHandler(button1_click);

To change this, you can make each of them like this (note that this does not need to be put in the Designer.cs file and that it is not recommended to manually edit this file):

button1.Click += new EventHandler(button_click);
button2.Click += new EventHandler(button_click);
button3.Click += new EventHandler(button_click);
button4.Click += new EventHandler(button_click);
...

where you have a method defined like this:

private void button_Click(object sender, EventArgs e)
{
   // stuff that happens when a button is clicked
}

This will make all of the buttons use the same button_click event handler. If you need to know which button fired the event, you can check the sender's id:

Button buttonThatClicked = sender as Button;
if (buttonThatClicked != null)
{
   // do whatever you need to, based on the button's properties
}


If you have duplicate code in several button_click methods just create a separate method and have the button_click methods call it.

private void button1_Click(object sender, EventArgs e)
        {
            this.ActiveControl.BackColor = controlColor;
            this.ActiveControl.Text = controlNumber;
            allCellsClicked[0] = '1';
            checkGuess();
        }

private void button2_Click(object sender, EventArgs e)
        {
            this.ActiveControl.BackColor = controlColor;
            this.ActiveControl.Text = controlNumber;
            allCellsClicked[0] = '2';
            checkGuess();
        }

private void checkGuess(){
       if (all_Buttons_Clicked())
            {
                allCellsClicked[0] = '0';
                allCellsClicked[1] = '0';
                allCellsClicked[2] = '0';
                allCellsClicked[3] = '0';
                button04.Enabled = false;
                button03.Enabled = false;
                button02.Enabled = false;
                .....
            }


You can just associate the same Click event callback function with every button's Click event. You can do this via code or you can do this via the Properties pane. Simply select each button and then pick the same event handler for each one.

In your case, if you want every button to use the button1_Click event, then associate that button1_Click event with each button. If you do that, you might want to rename the event callback function to something more generic.

0

精彩评论

暂无评论...
验证码 换一张
取 消