我想根據用戶輸入的行數和列數創建一個按鈕的網格,並且我的創建網格的方法不起作用。當我稱之爲網格不會被創建。如何通過在類中創建的方法在窗體上創建按鈕?
該方法在我的TileClass中,我試圖在我的GameBoard窗體中調用它。我覺得我不適當地使用課堂。我不認爲我正確調用方法,因爲我認爲這應該工作。
This is what the form looks like
class TileClass : Button
{
public const int LEFT = 20;
public const int WIDTH = 50;
public const int HEIGHT = 50;
public const int TOP = 50;
public const int VGAP = 30;
public int x;
public int y;
public int column;
public int row;
private int incomingRow;
private int incomingColumn;
public int IncomingRow { get => incomingRow; set => incomingRow = value; }
public int IncomingColumn { get => incomingColumn; set => incomingColumn = value; }
public TileClass()
{
}
public void CreateGrid()
{
x = LEFT;
y = TOP;
column = IncomingColumn;
row = IncomingRow;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
Button b = new Button();
b.Left = x;
b.Top = y;
b.Width = WIDTH;
b.Height = HEIGHT;
b.Text = j.ToString();
x += VGAP + HEIGHT;
this.Controls.Add(b);
}
}
}
}
遊戲鍵盤形式
public partial class GameBoard : Form
{
TileClass tileClass = new TileClass();
public GameBoard()
{
InitializeComponent();
}
private void txtEnter_Click(object sender, EventArgs e)
{
tileClass.IncomingColumn = int.Parse(txtColumn.Text);
tileClass.IncomingRow = int.Parse(txtRow.Text);
tileClass.CreateGrid();
}
這是什麼意思'不工作'?你有一個從類繼承的新類,它的方法創建了更多的按鈕。你不應該從按鈕繼承這個類。它應該從控制面板或組框中繼承。 –
哇,你去解決所有這些麻煩來解析值,並存儲'IncomingCollumn'和'IncomingRow',然後將它們傳遞給字段,然後將其傳遞給局部變量......然後你不會對它們做任何事情。你不覺得你的for循環應該看行列數嗎? –