2017-10-13 129 views
2

我想根據用戶輸入的行數和列數創建一個按鈕的網格,並且我的創建網格的方法不起作用。當我稱之爲網格不會被創建。如何通過在類中創建的方法在窗體上創建按鈕?

該方法在我的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(); 

    } 
+0

這是什麼意思'不工作'?你有一個從類繼承的新類,它的方法創建了更多的按鈕。你不應該從按鈕繼承這個類。它應該從控制面板或組框中繼承。 –

+1

哇,你去解決所有這些麻煩來解析值,並存儲'IncomingCollumn'和'IncomingRow',然後將它們傳遞給字段,然後將其傳遞給局部變量......然後你不會對它們做任何事情。你不覺得你的for循環應該看行列數嗎? –

回答

1

還有很多事情要做,以做到這一點:

class TileClass : Panel 
{ 
... 
    public int IncomingRow {get; set;} 
    public int IncomingColumn { get; set; } 
... 
} 

,並刪除:

private int incomingRow; 
private int incomingColumn; 

理想的方法是在添加按鈕之前使用ResumeLayout,並通過調用Invalidate來重繪Gameboard表單。 What does invalidate method do? 注:儘量山坳= 100,排= 100與不ResumeLayout &的Invalidate

public partial class GameBoard : Form 
{ 
    public GameBoard() 
    { 
     InitializeComponent(); 

     tileClass.Dock = DockStyle.Fill; 
     this.Controls.Add(tileClass); 
    } 

    TileClass tileClass = new TileClass(); 

    private void txtEnter_Click(object sender, EventArgs e) 
    { 

     tileClass.IncomingColumn = int.Parse(txtColumn.Text); 
     tileClass.IncomingRow = int.Parse(txtRow.Text); 

     this.ResumeLayout(); //Important 
     tileClass.CreateGrid(); 
     this.Invalidate(); // Important 
    } 
} 

和喜歡,它需要比這更多,你可以設置更多的屬性:

//tileClass.Location = new Point(10, 10); // not sure 
tileClass.Dock = DockStyle.Fill; 
//tileClass.Size = new Size(200, 200); // not sure 

和替代Ĵ < 5你應該使用列和行:

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 = string.Format("({0},{1})" , i, j); 

     x += VGAP + HEIGHT; 
     this.Controls.Add(b); 
    } 
    x = LEFT; // not sure, plz calculate! 
    y += Top * (i+1); // not sure, plz calculate! 
} 
+0

非常感謝,它的工作^^ – Kris

+0

快樂。請注意,您需要使用列和行值 –

+0

哦,是的,我知道,我有他們之前,我剛剛刪除他們,當我試圖讓它的工作,並沒有把他們回來 – Kris