当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Binding a Panel to a Form

问题描述:

I am making a Seabattle game where the Ships are hidden in a panel with a label array. This label array needs to have more or less rows and columns depending on the chosen difficulty level.

So I made a new class "Gameboard" where the Panel and the label array is defined.

The problem is that I cant figure out how to bind the panel I make in this class to the Form..

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());

}

}

}

If you'd like to see more classes please ask!

Thanks!

网友答案:

You could do one of these:

  1. Make 'GameBoard' a form, and then add the panel to that form. If you wanted to do this I would suggest creating a windows forms project (if you haven't already) and adding this code to the code for the main form (or a new form that you add) so that you have the designer components and all of the visual studio features of a form.

  2. Make it a usercontrol and add that usercontrol to a form. If you wanted to do this, I would add a new user control to the project and add this code to that usercontrol, again, so you get visual studio support.

  3. Create a new Form() inside of your gameboard and add it to that. (That's contrary to standard form practices in winforms, and you won't get as much IDE help, so I wouldn't suggest this for a new programmer).

分享给朋友:
您可能感兴趣的文章:
随机阅读: