我得到了一個製作一個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