2016-01-08 114 views
2

我正在尝试编写一个C#程序来播放跳棋。到目前为止,我已经完成了所有“浅”的事情,例如生成板和所有标签等。定义发件人动态添加的事件处理程序

我的问题是所有的控件都是动态添加的。所以我不能只是“双击”它们并且定义Button_Click事件要做什么。

这是我生成表单

public partial class formGameBoard : Form 
    { 
     private readonly int m_BoardSize; 
     private readonly string m_Player1Name; 
     private readonly string m_Player2Name; 
     private GameTile m_BlueTile; 
     private bool m_IsThereBlue; 

     public formGameBoard(int i_BoardSize, string i_Player1Name, string i_Player2Name) 
     { 
      m_BoardSize = i_BoardSize; 
      m_Player1Name = i_Player1Name; 
      m_Player2Name = i_Player2Name; 
      if (m_Player2Name == "(Computer)") 
      { 
       m_Player2Name = "Computer"; 
      } 
      m_IsThereBlue = false; 
      InitializeComponent(); 

     } 

     private void formGameBoard_Load(object sender, EventArgs e) 
     { 
      int SizeOfButton = 60; 
      int ButtonRowindex = 0; 
      int ButtonColindex = 0; 
      this.Size = new System.Drawing.Size(30 + m_BoardSize * SizeOfButton, 100 + m_BoardSize * SizeOfButton); 
      Button[,] PlayButtonArray = new Button[m_BoardSize, m_BoardSize]; 
      for (ButtonRowindex = 0; ButtonRowindex < m_BoardSize; ButtonRowindex++) 
      { 
       for (ButtonColindex = 0; ButtonColindex < m_BoardSize; ButtonColindex++) 
       { 
        PlayButtonArray[ButtonRowindex, ButtonColindex] = new Button(); 
        PlayButtonArray[ButtonRowindex, ButtonColindex].Size = new Size(SizeOfButton, SizeOfButton); 
        PlayButtonArray[ButtonRowindex, ButtonColindex].Left = 10 + ButtonRowindex * SizeOfButton; 
        PlayButtonArray[ButtonRowindex, ButtonColindex].Top = 50 + ButtonColindex * SizeOfButton; 
        if ((ButtonRowindex + ButtonColindex) % 2 == 0) 
        { 
         PlayButtonArray[ButtonRowindex, ButtonColindex].Enabled = false; 
         PlayButtonArray[ButtonRowindex, ButtonColindex].BackColor = Color.Gray; 
        } 

        this.Controls.Add(PlayButtonArray[ButtonRowindex, ButtonColindex]); 
       } 
      } 

      FillButtons(PlayButtonArray); 
     } 

     public void FillButtons(Button[,] ButtonMatrix) 
     { 
      int i, j; 
      for (i = 0; i < m_BoardSize; i++) 
      { 
       for (j = 0; j < m_BoardSize; j++) 
       { 
        if ((i + j) % 2 == 1) 
        { 
         if (j <= (m_BoardSize/2) - 2) 
         { 
          ButtonMatrix[i, j].Text = "O"; 
         } 

         if (j > (m_BoardSize/2)) 
         { 
          ButtonMatrix[i, j].Text = "X"; 
         } 
        } 
       } 
      } 
     } 

     struct GameTile 
     { 
      int RowIndex; 
      int ColumnIndex; 
     } 
    } 
} 

,我与它没有任何问题的代码,它看起来在我看来很不错 enter image description here

我的问题是,所有的按钮都动态地添加。我没有拖放它们。我在表单加载时创建了它们。现在,当我点击一个按钮时,我想要发生一些事情。例如,我想让我点击的按钮将其颜色更改为蓝色。

我该怎么做?

+0

您可以创建一个方法和挂钩与方法的按钮。例如。 button.OnClicked + = OnClicked –

+0

您可以为该按钮或按钮创建代理OnCleick事件 – MethodMan

回答

2

您可以添加处理程序到Click事件的按钮并使用sender参数。

也可能数组中按钮的索引对您很重要,因此您可以将按钮的数组索引存储在Tag属性中并稍后使用它。

在for循环:

var button = PlayButtonArray[ButtonRowindex, ButtonColindex]; 
button.Tag= new Point(ButtonRowindex, ButtonColindex); 
button.Click += Button_Click; 

代码Button_Click:

private void Button_Click(object sender, EventArgs e) 
{ 
    var button = sender as Button; 
    //You can manipulate button here 

    //Also to extract the button index in array: 
    var indexes = (Point)button.Tag; 

    MessageBox.Show(string.Format("This is the button at {0}, {1}", indexes.X, indexes.Y)); 
} 
+0

它正在工作!谢谢! –

+0

在for循环中,您正在创建按钮。在'PlayButtonArray [ButtonRowindex,ButtonColindex]'之后或者使用'PlayButtonArray [ButtonRowindex,ButtonColindex]'代替var按钮。 –

+0

button.Tag作为点似乎没有工作,虽然。点是非空值类型,并且必须与引用类型或可为空值类型一起使用。 –

4

添加单击事件的按钮是非常简单的,就像这样:

buttonName.Click += (sender, e) => { buttonName.Foreground = Colors.Blue; }; 

或者,如果你想它,将处理许多按钮点击的方法,你可以这样做:

buttonName.Click += YourButtonHandler; 

private void YourButtonHandler(object sender, EventArgs e) 
{ 
    // do something, for example: 
    Button b = sender as Button; 
    b.Foreground = Colors.Blue; 
} 
+0

谢谢!多谢。 –

相关问题