2014-10-03 31 views
1

我得到了一個製作一個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:如果需要,我可以上傳解決方案

+0

。創建委託後,請在事件處理程序中調用PN1(some_text_box)和PN2(some_text_box)。話雖如此,涉及屬性的解決方案(如下)更加清晰和更好。代表們非常有幫助,但似乎不適合你的用例,並且不必要地使事情複雜化。 – kha 2014-10-03 15:42:31

回答

1

這個問題已經討論過幾千次,但每次都有點不同。
在這種情況下,您可以在newgameform中創建兩個公共屬性。
當用戶單擊確定按鈕時,您將使用newgameform上文本框的值設置屬性。
然後很容易從主窗體中讀取它們。

public string Player1 {get; private set;} 
public string Player2 {get; private set;} 

private void btnOK_Click(object sender, EventArgs e) 
{ 
    this.Player1 = txtBoxForPlayer1.Text; 
    this.Player2 = txtBoxForPlayer2.Text; 
    this.DialogResult = DialogResult.OK; 
} 

和你沒有正確使用您的代表主要形式

public void StartGame() 
{ 
    using(frmNewGame OpenForm = new frmNewGame()) 
    { 
     if(DialogResult.OK == OpenForm.ShowDialog()) 
     { 
      lblName1.Text = OpenForm.Player1; 
      lblName2.Text = OpenForm.Player2; 
      lblName1.Visible = true; 
      lblName2.Visible = true; 
      ..... 
     } 
    } 
} 
+0

非常感謝你的朋友,它的工作很好:) – 2014-10-03 15:55:17

+0

很高興能有所幫助。作爲一個相對較新的用戶,我希望建議閱讀[接受答案如何工作](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Steve 2014-10-03 16:09:52

+0

雖然我只能接受答案,一旦我達到15個代表點,再次感謝:) – 2014-10-05 11:26:11

相關問題