0

I am making a chess game and I am using array of buttons dynamically so, I am using a single click event for all of them. I want to ask user for inputs on button's click event(where the user wants to put the piece once the button is clicked). How can I do that as I am new to C# and have tried a lot but couldn't figure it out.

Here's my code:

    private void Btn_Click(object sender, EventArgs e)
    {
        //for black pieces

        
        Button btn2 = new Button();
        btn2 = sender as Button;
        int k;
        int l;
        for (int i=0; i<8; i++)
        {
            for(int j=0; j<8; j++)
            {
                if (btn2.BackgroundImage == blackpawn)
                {
                    if (btn2 == btn[i, j])
                    {
                        //here i want to ask user where he wants to put the piece
                        btn[i, j].BackgroundImage = null;
                        k = ++i;
                        l = j;
                        btn[k, l].BackgroundImage = blackpawn;
                        btn[k, l].BackgroundImageLayout = ImageLayout.Stretch;

                    }   
                }
            }
        }
        
    }
Package.JSON
  • 301
  • 5
  • 25
  • you don't need to ask user input explicitly. on the first click user select one piece. on the second click user select a new position of that piece. repeat for both users in turns until the game is over – ASh Jun 02 '17 at 13:37
  • /*on the first click user select one piece. on the second click user select a new position of that piece.*/ I don't know how to do that. That's exactly what i have asked in my question. – Package.JSON Jun 02 '17 at 13:44

3 Answers3

0

I cant tell if you are struggling to create the events on dynamic buttons of just find a button so ill do both.

When creating the button, you need to add the event to it.

Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);

//protected void button_Click (object sender, EventArgs e) { }

(cited from here : How can i create dynamic button click event on dynamic button?)

Or for finding that button maybe iterating through the controls that are on the form and identifying the one you want?

foreach (Control item in this.Controls)
       {
          if (item.GetType() == typeof(Button) && item.Name == "random coordinate")
               {
                 //Do WORK
               }
       }
thanatorr
  • 103
  • 1
  • 9
  • I am not iterating through buttons i have done that. I want to ask how to ask for user input to move the picture of a button on first click to the other button which the user has clicked second time. – Package.JSON Jun 02 '17 at 13:42
  • in that case i think using a Button is not the "proper" way to do this. But you could simply handle the on mouse click events and get the Coordinate of the mouse at that time. Record it and then set the button to that position. – thanatorr Jun 02 '17 at 13:50
0

You don't have to do anything complicated, something like this will work

bool firstClick = true;

private void Btn_Click(object sender, EventArgs e)
{
    if (firstClick)
    {
        // Do the stuff you'd like to do on the first click.

        firstClick = false; // Remember to set firstClick to false.
    }
    else
    {
        // Do the stuff you'd like to do on the second click.

        firstClick = true; // Remember to set firstClick to true.
    }
}
Gareth
  • 913
  • 6
  • 14
0

Maybe keeping reference of source and target button will help:

Button FirstButton = null, SecondButton = null;

        private void ButtonSelected(object sender, EventArgs e)
        {
            if(FirstButton == null && SecondButton == null) // no button already selected
            {
                FirstButton = sender as Button;
            }
            else if(FirstButton != null && SecondButton == null) // one button selected, its second's turn
            {
                SecondButton = sender as Button;
                // your code here to move chess pieces from first button to second button
                SecondButton.BackgroundImage = FirstButton.BackgroundImage;
                FirstButton.BackgroundImage = null;
                // and some other stuff

                FirstButton = null; SecondButton = null; // Set both buttons to null after one piece move.
            }
        }
Usama Aziz
  • 169
  • 1
  • 10