如何通过在类中创建的方法在窗体上创建按钮?

问题描述:

我想根据用户输入的行数和列数创建一个按钮的网格,并且我的创建网格的方法不起作用。当我称之为网格不会被创建。如何通过在类中创建的方法在窗体上创建按钮?

该方法在我的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循环应该看行列数吗? –

还有很多事情要做,以做到这一点:

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