简体   繁体   中英

How to use a button sender after another button was pressed?

So I am making a chess game (The board is 64 buttons with the same sender) and What I want to do is after he pressed the second button if the move is legal it will set the first button background image to null and the second to the first:

public void button_click(object sender, EventArgs e)
    {
        if (partOfTurn == false)
        {
            for (int x = 0; x <= 7; x++)
            {
                for (int y = 0; y <= 7; y++)
                {
                    if (Buttons[x, y] == ((Button)sender))
                    {
                        Ax = x;
                        Ay = y;
                    }
                }
            }
            place_holder.BackgroundImage = ((Button)sender).BackgroundImage;
            partOfTurn = true;
        }

        else if (partOfTurn == true)
        {
            for (int x = 0; x <= 7; x++)
            {
                for (int y = 0; y <= 7; y++)
                {
                    if (Buttons[x, y] == ((Button)sender))
                    {
                        Bx = x;
                        By = y;
                    }
                }
            }
            click();
            partOfTurn = false;
        }

        void click()
        {
            if (turn == true)
            {
                if (place_holder.BackgroundImage == Properties.Resources.White_Pown)
                {
                    if (Bx == Ax + 1 && By == Ay + 1 || Bx == Ax - 1 && By == Ay + 1)
                    {
                        if (((Button)sender).BackgroundImage == Properties.Resources.Black_Pown||
                            ((Button)sender).BackgroundImage == Properties.Resources.Black_Rook||
                            ((Button)sender).BackgroundImage == Properties.Resources.Black_Knight||
                            ((Button)sender).BackgroundImage == Properties.Resources.Black_Bishop||
                            ((Button)sender).BackgroundImage == Properties.Resources.Black_Queen||
                            ((Button)sender).BackgroundImage == Properties.Resources.Black_King)
                        {
                            //set the background image of the first to null and of the other button to the first.
                        }
                    }
                }
            }
        }
    }

But in order to do so I need to use the (Button)sender of the second but also of the first to clear it.

I tried to work around it and save the background button on a place holder so I can see what was on the button that was pressed but I still need to clear the first button

Any ideas?

To recognise two buttons in sequence you need to store a reference to the first button as a class-level variable, such as previousButton. Assign this at the end of the click code so that you can refer to it on the next click.

public partial class Form1 : Form
{
    private Button previousButton = null;

    public Form1()
    {
        InitializeComponent();

        button1.Click += Buttons_Click;
        button2.Click += Buttons_Click;
    }

    private void Buttons_Click(object sender, EventArgs e)
    {
        if (previousButton != null)
        {
            // do something with previousButton
        }

        // code to work with the currently clicked button
        // as (Button)sender

        // remember the current button
        previousButton = (Button)sender;
    }
}

If the button sequence always happens in pairs then, once a paired-sequence has completed, set previousButton back to null . This tells you that a new 'sequence' will begin.

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