我得到了一个制作一个tic tac toe游戏(大小为8 * 8)的任务,到目前为止我设法编写了主游戏,但是我在传递值时遇到了问题对于从新游戏形式进入mainform的玩家名称,任何人都可以帮忙吗?如何将文本框中的值传递给不同形式的标签
代码的MainForm
public void NameValue1(TextBox NV1)
{
lblName1.Text = NV1.Text;
}
public void NameValue2(TextBox NV2)
{
lblName2.Text = NV2.Text;
}
private void mnuNewGame_Click(object sender, EventArgs e)
{
iFlag = 1; // This is used to run the paint function on the form
StartGame();
for (int i = 0; i < 64; i++)
{
this._btn[i].Click += new System.EventHandler(this.ClickControl);
this._btn[i].MouseMove += new MouseEventHandler(this.MoveControl); // DEBUG // these 2 allow me to check 'does my mouse hover on the right box?'
this._btn[i].MouseLeave += new System.EventHandler(this.LeaveControl); // DEBUG //
}
}
public void StartGame()
{
frmNewGame OpenForm = null;
OpenForm = new frmNewGame();
OpenForm.ShowDialog();
//lblName1.Text = "Player 1"; // PLACEHOLDER // these 2 are what I'm trying to replace to accept values from newgameform
//lblName2.Text = "Player 2"; // PLACEHOLDER //
lblName1.Visible = true;
lblName2.Visible = true;
Random RandomNumber = new Random();
int Start = RandomNumber.Next(0, 3);
if (Start == 1)
{
PlayerTurn = "Player1";
lblName1.Font = new Font("Microsoft Sans Serif", 17, FontStyle.Underline | FontStyle.Bold);
lblName2.Font = new Font("Microsoft Sans Serif", 17, FontStyle.Regular | FontStyle.Bold);
}
else if (Start == 2)
{
PlayerTurn = "Player2";
lblName1.Font = new Font("Microsoft Sans Serif", 17, FontStyle.Regular | FontStyle.Bold);
lblName2.Font = new Font("Microsoft Sans Serif", 17, FontStyle.Underline | FontStyle.Bold);
}
NumberTurn = 0;
for (int i = 0; i < 64; i++)
{
_btn[i].Text = "";
_btn[i].BackColor = Color.Yellow;
_btn[i].Visible = true;
_btn[i].Enabled = true;
}
}
代码newgameform
public delegate void PassName1(TextBox tbxPlayerName1);
public delegate void PassName2(TextBox tbxPlayerName2);
private void btnOK_Click(object sender, EventArgs e)
{
frmConnectFour frmGame = new frmConnectFour();
PassName1 PN1 = new PassName1(frmGame.NameValue1);
PassName2 PN2 = new PassName2(frmGame.NameValue2);
this.DialogResult = DialogResult.OK;
}
正如你所看到的,我已经预习形式之间的连接读取玩家姓名文本框(没有错误),但我得到了问题如何将它传递给mainform上的startgame()函数
PS:如果需要,我可以上传解决方案
。创建委托后,请在事件处理程序中调用PN1(some_text_box)和PN2(some_text_box)。话虽如此,涉及属性的解决方案(如下)更加清晰和更好。代表们非常有帮助,但似乎不适合你的用例,并且不必要地使事情复杂化。 – kha 2014-10-03 15:42:31