Relatively new to C#. I have to make a Tic Tac Toe. I am thinking of using only one method to change my button properties.
This is what I imagine.
int count = 0;
private void button1_Click(object sender, EventArgs e)
{
ChangeButton(count);
}
public int ChangeButton(int i)
{
if(count % 2 == 0)
{
// button.text = x
// i want to be able to change the text of whichever button is clicked
}
else
{
// button.text = o
}
// button.enable = false
// I want to disable whichever button is clicked
i ;
return i;
}
I don't know should I do the // parts. Hope you can help me. Thanks!
CodePudding user response:
If you have several buttons all calling the same click event, you can identify the button from the sender parameter:
Button btn = sender as Button;
Then you can change the text for that button:
btn.Text = count % 2 == 0 ? "x" : "o";
All in your click event:
int count = 0;
private void button1_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
btn.Text = count % 2 == 0 ? "x" : "o";
btn.Enabled = false;
}