Results 1 to 3 of 3

Thread: Help needed to dynamically add a button to a cell in a TableLayoutPanel:

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile Help needed to dynamically add a button to a cell in a TableLayoutPanel:

    Hello everybody,

    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);
    
                }
    Last edited by The Thing; Dec 6th, 2006 at 06:52 PM.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile Re: Help needed to dynamically add a button to a cell in a TableLayoutPanel:

    Thank you jmcilhinney, I was struggling with that for ages.

    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.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width