我试图将我的方法改为班级。我做到了,但有些工作没有发生:
传递(object sender, EventArgs e)
的正确方法是什么?
这是使用该方法的代码:
public void GetPlayerChoice_Click(object sender, EventArgs e)
{
ChangeProperties(sender, e);
PlayerWon();
}
这是方法:
//Change the properties of the buttond and cheack who's turn is it.
public void ChangeProperties(object sender, EventArgs e)
{
var btn = sender as Button;
if (playerX.Turn == true)
{
btn.Text = playerX.Name;
btn.BackColor = Color.FromArgb(255, 15, 0);
playerX.Turn = false;
playerO.Turn = true;
buttonUser1.BackColor = Color.FromArgb(255, 156, 53);
buttonUser2.BackColor = Color.FromArgb(255, 255, 255);
}
else
{
btn.Text = playerO.Name;
btn.BackColor = Color.FromArgb(96, 255, 40);
playerX.Turn = true;
playerO.Turn = false;
buttonUser1.BackColor = Color.FromArgb(255, 255, 255);
buttonUser2.BackColor = Color.FromArgb(255, 156, 53);
}
btn.Enabled = false;
}
//Cheack if there is a winer.
public void PlayerWon()
{
if ((button1.Text == button2.Text && button2.Text == button3.Text) || //horizontal
(button4.Text == button5.Text && button5.Text == button6.Text) || //horizontal
(button7.Text == button8.Text && button8.Text == button9.Text) || //horizontal
(button1.Text == button4.Text && button4.Text == button7.Text) || //vertical
(button2.Text == button5.Text && button5.Text == button8.Text) || //vertical
(button3.Text == button6.Text && button6.Text == button9.Text) || //vertical
(button1.Text == button5.Text && button5.Text == button9.Text) || //slanted
(button3.Text == button5.Text && button5.Text == button7.Text)) //slanted
{
EnableAllButtonFalse();
if (playerX.Turn == true)
{
MessageBox.Show("O is the winner");
playerO.Score += 1;
}
if (playerX.Turn == false)
{
MessageBox.Show("X is the winner");
playerX.Score += 1;
}
NewGame();
}
}
//Enable false all button in the game.
public void EnableAllButtonFalse()
{
foreach (Control c in Controls)
{
if (c is Button)
{
c.Enabled = false;
}
}
}
//Enable true all button in the game.
public void EnableAllButtonTrue()
{
foreach (Control c in Controls)
{
if (c is Button)
{
c.Enabled = true;
}
}
}
public void NewGame()
{
EnableAllButtonTrue();
if(playerX.Turn == false)
{
playerX.Turn = true;
playerO.Turn = false;
}
else
{
playerX.Turn = false;
playerO.Turn = true;
}
}
当我改变我的程序以使用它没有正常工作的方法时。 它只做了一些代码。我无法理解它为什么会发生。
这是使用该类的代码:
public void GetPlayerChoice_Click(object sender, EventArgs e)
{
ExtensionMethods ex = new ExtensionMethods();
ex.Sender = sender;
ex.E = e;
ex.ChangeProperties(sender, e);
ex.PlayerWon();
}
这是班级:
class ExtensionMethods : Form1
{
private object sender;
private EventArgs e;
public object Sender { get; set; }
public EventArgs E { get; set; }
//Change the properties of the buttond and cheack who's turn is it.
public void ChangeProperties(object sender, EventArgs e)
{
var btn = sender as Button;
if (playerX.Turn == true)
{
btn.Text = playerX.Name;
btn.BackColor = Color.FromArgb(255, 15, 0);
playerX.Turn = false;
playerO.Turn = true;
buttonUser1.BackColor = Color.FromArgb(255, 156, 53);
buttonUser2.BackColor = Color.FromArgb(255, 255, 255);
}
else
{
btn.Text = playerO.Name;
btn.BackColor = Color.FromArgb(96, 255, 40);
playerX.Turn = true;
playerO.Turn = false;
buttonUser1.BackColor = Color.FromArgb(255, 255, 255);
buttonUser2.BackColor = Color.FromArgb(255, 156, 53);
}
btn.Enabled = false;
}
//Cheack if there is a winer.
public void PlayerWon()
{
if ((button1.Text == button2.Text && button2.Text == button3.Text) || //horizontal
(button4.Text == button5.Text && button5.Text == button6.Text) || //horizontal
(button7.Text == button8.Text && button8.Text == button9.Text) || //horizontal
(button1.Text == button4.Text && button4.Text == button7.Text) || //vertical
(button2.Text == button5.Text && button5.Text == button8.Text) || //vertical
(button3.Text == button6.Text && button6.Text == button9.Text) || //vertical
(button1.Text == button5.Text && button5.Text == button9.Text) || //slanted
(button3.Text == button5.Text && button5.Text == button7.Text)) //slanted
{
EnableAllButtonFalse();
if (playerX.Turn == true)
{
MessageBox.Show("O is the winner");
playerO.Score += 1;
}
if (playerX.Turn == false)
{
MessageBox.Show("X is the winner");
playerX.Score += 1;
}
NewGame();
}
}
//Enable false all button in the game.
public void EnableAllButtonFalse()
{
foreach (Control c in Controls)
{
if (c is Button)
{
c.Enabled = false;
}
}
}
//Enable true all button in the game.
public void EnableAllButtonTrue()
{
foreach (Control c in Controls)
{
if (c is Button)
{
c.Enabled = true;
}
}
}
public void NewGame()
{
EnableAllButtonTrue();
if(playerX.Turn == false)
{
playerX.Turn = true;
playerO.Turn = false;
}
else
{
playerX.Turn = false;
playerO.Turn = true;
}
}
答案 0 :(得分:1)
我简化了您的代码,并且能够让它运行起来。 Here is a .NETFiddle of it working
这是代码。
using System;
public class ExtensionMethods
{
public static void Main()
{
ExtensionMethods em = new ExtensionMethods();
em.PlayerWon();
}
public void PlayerWon()
{
NewGame();
}
public void NewGame()
{
Console.Write("Starting New Game");
}
}
要使用发件人,您需要执行以下操作:
void HandleMyButton(object sender, EventArgs e)
{
// this method can have some arguments, if needed
DoButtonWork();
}