我正在尝试编写一个玩跳棋的C#程序。到目前为止,我已经完成了所有" Shallow"诸如生成电路板和所有标签之类的东西等。
我的问题是所有控件都是动态添加的。所以我不能只是双击"他们并定义了在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;
}
}
}
我的问题是所有这些按钮都是动态添加的。我没有拖放他们。我在表单加载时创建了它们。现在,当我点击一个按钮时,我想要发生一些事情。例如,我想要单击的按钮将其颜色更改为蓝色。
我该怎么做?
答案 0 :(得分:4)
为按钮添加click事件非常简单,如下所示:
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged = delegate { };
protected void OnPropertyChanged(string property)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
#endregion
private ObservableCollection<animalTypes1> _source1 = new ObservableCollection<animalTypes1> {
new animalTypes1() { type1name = "Mammals" } };
public ObservableCollection<animalTypes1> Source1
{
get { return _source1; }
}
private ObservableCollection<animalTypes2> _source2 = new ObservableCollection<animalTypes2> {
new animalTypes2() { type2name = "Reptiles" } };
public ObservableCollection<animalTypes2> Source2
{
get { return _source2; }
}
private bool _isSource2;
public bool IsSource2
{
get { return _isSource2; }
set
{
if (value != _isSource2)
{
_isSource2 = value;
OnPropertyChanged("IsSource2");
}
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void btn1_click(object sender, RoutedEventArgs e)
{
IsSource2 = false;
}
private void btn_click2(object sender, RoutedEventArgs e)
{
IsSource2 = true;
}
}
或者如果你想让它成为一个可以处理许多按钮点击的方法,你可以这样做:
buttonName.Click += (sender, e) => { buttonName.Foreground = Colors.Blue; };
答案 1 :(得分: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));
}