2014-10-03 32 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

相关问题