我正在制作一个 Seabattle 游戏,其中船舶隐藏在带有标签数组的面板中。这个标签数组需要有更多或更少的行和列,具体取决于所选的难度级别。
所以我创建了一个新类“游戏板”,其中定义了面板和标签数组。问题是我不知道如何将我在此类中制作的面板绑定到表单..
namespace SO_S2_Programmeren_Groep08 {
class GameBoard{
Panel pnlSlagveld;
private Label[,] lblArray;
private int row;
private int column;
public Label[,] LblArray {
get {
return lblArray;
}
set {
lblArray = value;
}
}
public int Row {
get { return row; }
set { row = value; }
}
public int Column {
get { return column; }
set { column = value; }
}
public GameBoard(int row, int column) {
this.row = row;
this.column = column;
CreateLableArray(row, column);
}
public GameBoard() {
this.row = 7;
this.column = 9;
CreateLableArray(row, column);
}
private void CreateLableArray(int ingrow, int ingcolumn) {
pnlBattleField = new System.Windows.Forms.Panel();
lblArray = new Label[ingrow, ingcolumn];
int xpos = 0;
int ypos = 0;
for (int row = 0; row < ingrow; row++) {
for (int column = 0; column < ingcolumn; column++) {
lblArray[row, column] = new Label();
lblArray[row, column].Left = xpos;
lblArray[row, column].Top = ypos;
lblArray[row, column].Width = 50;
lblArray[row, column].Height = 50;
lblArray[row, column].Tag = (char)('A' + column) + (row + 1).ToString();
lblArray[row, column].Click += lblArray_Click;
lblArray[row, column].BackColor = Color.Aqua;
lblArray[row, column].BorderStyle = BorderStyle.FixedSingle;
pnlBattlefield.Controls.Add(lblArray[row, column]);
xpos += lblArray[row, column].Width;
}
ypos += lblArray[row, 0].Width;
xpos = 0;
}
}/*CreateLableArray*/
private void lblArray_Click(object sender, EventArgs e) {
MessageBox.Show("Clicked on Label " + ((Label)sender).Tag.ToString());
}
}
}
如果你想看更多的课程,请询问!
谢谢!