C# windows窗体应用程序中的Doughts和cross
有人能告诉我为什么我不能运行我的程序吗?我尝试在windows窗体应用程序中创建doughts和Cross我尝试更改代码和其他内容,但我尝试了所有操作,但我认为底部的函数有问题。到现在为止,我想编程运行,生成9个按钮,当我点击它们时,会出现一个“X”或“O”,这取决于轮到谁了 另外,我还没有添加win condition函数,我想测试程序是否正常工作 提前谢谢C# windows窗体应用程序中的Doughts和cross,c#,forms,C#,Forms,有人能告诉我为什么我不能运行我的程序吗?我尝试在windows窗体应用程序中创建doughts和Cross我尝试更改代码和其他内容,但我尝试了所有操作,但我认为底部的函数有问题。到现在为止,我想编程运行,生成9个按钮,当我点击它们时,会出现一个“X”或“O”,这取决于轮到谁了 另外,我还没有添加win condition函数,我想测试程序是否正常工作 提前谢谢 public partial class Form1 : Form { Button[] gameButtons = new B
public partial class Form1 : Form
{
Button[] gameButtons = new Button[9]; //array of buttons for markers(X's and O's)
bool cross = true; //cross is set to true if the next marker is to be a cross
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "More Complex Version of Noughts and Crosses";
this.BackColor = Color.BlanchedAlmond;
this.Width = 400;
this.Height = 400;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 3) * 50;
int y = 50 + (i / 3) * 50;
this.gameButtons[i].Location = new System.Drawing.Point(x, y);
this.gameButtons[i].Name = "btn" + (index + 1);
this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
this.gameButtons[i].TabIndex = i;
//this.gameButtons[i].Text = Convert.ToString(index);
this.gameButtons[i].UseVisualStyleBackColor = true;
this.gameButtons[i].Visible = true;
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender,index);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender,int i)
{
if (((Button)sender).Text == "")
{
if (cross == true)
{
((Button)sender).Text = "X";
gameButtons[i] = 'X';
}
else
{
((Button)sender).Text = "O";
gameButtons[i] = 'O';
}
cross = !cross;
}
}
}
如果错误是“无法将类型char隐式转换为system.windows.forms.button”首先,您有一些编译错误:
// These two lines both throw the error:
// Cannot implicitly convert type 'char' to 'System.Windows.Forms.Button'
gameButtons[i] = 'X';
gameButtons[i] = 'O';
这是因为您试图为角色设置按钮。这不是必需的,因为您已经更改了按钮的文本,可以删除这些行
下一步,当单击按钮时,您会得到一个运行时异常,在这一行:
// The following line fails with the error:
// Unable to cast object of type 'WinFormTest.Form1' to type 'System.Windows.Forms.Button'.
if (((Button)sender).Text == "")
if (gameButtons[a] == gameButtons[b] && . . .
这是因为sender
是Form1
对象,而不是按钮。原因是,在将事件分配给按钮单击时,您将sender
而不是sender1
传递给事件,并且由于此事件的分配是在表单中进行的。Load
事件中,sender
是表单1
。因此,您需要将分配改为传递sender1
:
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender1, index);
您遇到的下一个问题(因为您已经修改了原始问题中的代码)是
threeInARow
方法:
if (gameButtons[a] = 'X') // Error: Cannot implicitly convert type 'char' to 'Button'
原因是gameButtons
是一个Button
对象的数组,因此gameButtons[a]
表示一个按钮
,您不能将字符'X'
分配给按钮
(它们是两种不同的类型)。由于您已经为每个按钮(属于string
类型)的Text
属性指定了一个值,因此您可以使用该值
此外,您正在使用一个=
符号,这是一个赋值。您需要进行比较,它使用双=
符号。所以,把这些放在一起,你会得到:
if (gameButtons[a].Text == "X")
您已将类似的有问题的代码添加到您的按钮hasbeenpressed
方法中,您只需删除该方法,因为我们可以比较文本
属性,并且不需要此附加赋值:
gameButtons[i] = 'X'; // Remove these invalid assignments
这解决了编译错误,但在与此行进行比较时仍然存在另一个问题:
// The following line fails with the error:
// Unable to cast object of type 'WinFormTest.Form1' to type 'System.Windows.Forms.Button'.
if (((Button)sender).Text == "")
if (gameButtons[a] == gameButtons[b] && . . .
此行询问索引a
处数组中的按钮引用是否指向与索引b
处数组中的按钮引用完全相同的对象。这种情况永远不会发生,因为您(正确地)用9个唯一的按钮初始化了数组
您真正想做的是比较每个按钮的文本
属性,如下所示:
if (gameButtons[a].Text == gameButtons[b].Text && . . .
最后,您已经将名为sender
的对象
参数包含到您不使用的方法中,因此您最好将其删除(或者在方法中使用它)
所以,把这些放在一起,你有:
private void threeInARow(int a, int b, int c)
{
if (gameButtons[a].Text == gameButtons[b].Text && gameButtons[a].Text == gameButtons[c].Text)
{
if (gameButtons[a].Text == "X")
{
MessageBox.Show("the winner is crosses");
}
else
{
MessageBox.Show("the winner is noughts");
}
}
}
首先,您有一些编译错误:
// These two lines both throw the error:
// Cannot implicitly convert type 'char' to 'System.Windows.Forms.Button'
gameButtons[i] = 'X';
gameButtons[i] = 'O';
这是因为您试图为角色设置按钮。这不是必需的,因为您已经更改了按钮的文本,可以删除这些行
下一步,当单击按钮时,您会得到一个运行时异常,在这一行:
// The following line fails with the error:
// Unable to cast object of type 'WinFormTest.Form1' to type 'System.Windows.Forms.Button'.
if (((Button)sender).Text == "")
if (gameButtons[a] == gameButtons[b] && . . .
这是因为sender
是Form1
对象,而不是按钮。原因是,在将事件分配给按钮单击时,您将sender
而不是sender1
传递给事件,并且由于此事件的分配是在表单中进行的。Load
事件中,sender
是表单1
。因此,您需要将分配改为传递sender1
:
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender1, index);
您遇到的下一个问题(因为您已经修改了原始问题中的代码)是threeInARow
方法:
if (gameButtons[a] = 'X') // Error: Cannot implicitly convert type 'char' to 'Button'
原因是gameButtons
是一个Button
对象的数组,因此gameButtons[a]
表示一个按钮
,您不能将字符'X'
分配给按钮
(它们是两种不同的类型)。由于您已经为每个按钮(属于string
类型)的Text
属性指定了一个值,因此您可以使用该值
此外,您正在使用一个=
符号,这是一个赋值。您需要进行比较,它使用双=
符号。所以,把这些放在一起,你会得到:
if (gameButtons[a].Text == "X")
您已将类似的有问题的代码添加到您的按钮hasbeenpressed
方法中,您只需删除该方法,因为我们可以比较文本
属性,并且不需要此附加赋值:
gameButtons[i] = 'X'; // Remove these invalid assignments
这解决了编译错误,但在与此行进行比较时仍然存在另一个问题:
// The following line fails with the error:
// Unable to cast object of type 'WinFormTest.Form1' to type 'System.Windows.Forms.Button'.
if (((Button)sender).Text == "")
if (gameButtons[a] == gameButtons[b] && . . .
此行询问索引a
处数组中的按钮引用是否指向与索引b
处数组中的按钮引用完全相同的对象。这种情况永远不会发生,因为您(正确地)用9个唯一的按钮初始化了数组
您真正想做的是比较每个按钮的文本
属性,如下所示:
if (gameButtons[a].Text == gameButtons[b].Text && . . .
最后,您已经将名为sender
的对象
参数包含到您不使用的方法中,因此您最好将其删除(或者在方法中使用它)
所以,把这些放在一起,你有:
private void threeInARow(int a, int b, int c)
{
if (gameButtons[a].Text == gameButtons[b].Text && gameButtons[a].Text == gameButtons[c].Text)
{
if (gameButtons[a].Text == "X")
{
MessageBox.Show("the winner is crosses");
}
else
{
MessageBox.Show("the winner is noughts");
}
}
}
见下面的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Button[] gameButtons = new Button[9]; //array of buttons for markers(X's and O's)
bool cross = true; //cross is set to true if the next marker is to be a cross
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "More Complex Version of Noughts and Crosses";
this.BackColor = Color.BlanchedAlmond;
this.Width = 400;
this.Height = 400;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 3) * 50;
int y = 50 + (i / 3) * 50;
this.gameButtons[i].Location = new System.Drawing.Point(x, y);
this.gameButtons[i].Name = "btn" + (index + 1);
this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
this.gameButtons[i].TabIndex = i;
//this.gameButtons[i].Text = Convert.ToString(index);
this.gameButtons[i].UseVisualStyleBackColor = true;
this.gameButtons[i].Visible = true;
gameButtons[i].Click += new EventHandler(buttonHasBeenPressed);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender, EventArgs e)
{
if (((Button)sender).Text == "")
{
if (cross == true)
{
((Button)sender).Text = "X";
//gameButtons[i].Text = "X";
}
else
{
((Button)sender).Text = "O";
//gameButtons[i].Text = 'O';
}
cross = !cross;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统。