Help needed to dynamically add a button to a cell in a TableLayoutPanel:
Hello everybody, :wave:
I am trying to dynamically add a button to a TableLayoutPanel at a particular column and row location, but I don't seem to be having much luck so far.
The following code compiles, but I don't see anything on my form (Form1). Any help would be much appreciated in solving the problem.
What I would like to eventually have working is a routine that would use two arrays of integer values for the cell positions - (in conjunction with an array of Button objects) to determine where in the TableLayoutPanel my Button objects would be added.
Code:
//This is just a test to get the basics working first!!!
public Form1()
{
InitializeComponent();
Button MyButton = new Button();
MyButton.Visible = true;
TableLayoutPanel MyPanel = new TableLayoutPanel();
TableLayoutPanelCellPosition MyCellPosition = new TableLayoutPanelCellPosition();
MyCellPosition.Column = 1;
MyCellPosition.Row = 1;
MyPanel.SetCellPosition(MyButton, MyCellPosition);
}
Re: Help needed to dynamically add a button to a cell in a TableLayoutPanel:
SetCellPosition sets the cell position of a child control. If your Button is not a child coltrol the you can't set its cell position. You have to add the Button to the TLP before you can set its cell position:
Code:
MyPanel.Controls.Add(MyButton);
MyPanel.SetCellPosition(MyButton, MyCellPosition);
Re: Help needed to dynamically add a button to a cell in a TableLayoutPanel:
Thank you jmcilhinney, I was struggling with that for ages. :blush:
Previously I've coded a simple puzzle game in Java and I want to rewrite it now using Visual C#, but my coding skills are getting rusty for want of usage. For efficiency I need to be able to dynamically create and access the buttons that form part of my puzzle, hence the reason for my post.
Thanks again jmcilhinney, and happy Christmas to you and yours. :)