locked
TableLayoutPanel - Runtime manipulation

    Question

  • I have designed a TableLayoutPanel with excessive controls in it. Is there any way in which I can access the controls of a particular row or column.
    Like we have a statement

    TableLayoutPanel1.Controls

    What I want to know is that how can i make it

    TableLayoutPanel1.Columns(0).Controls
    or

    TableLayoutPanel1.Rows(0).Controls
    Tuesday, November 21, 2006 11:49 AM

Answers

  • Well, it's been quite sometime since I made this post. I could not find a direct solution to my problem, but there are workarounds you can use.

    Like, you can construct an array of child controls for the particular column or row of the TableLayoutPanel (you would be able to do this, only if you know the name of controls or their index in the TableLayoutPanel). You can then use this array in any way you want. Remember, before accessing any property that does not belong to the Control class itself on a element of this array, use TypeOf & Is opertor (in VB.NET) to make sure you are not calling an invalid property on a control (unless offcourse you are sure that you are calling a valid property on the element).

    For my purpose, I found a workaround using Controls collection of the TableLayoutPanel. You can use this collection to index the controls either by their name, or by their position in the collection.

    There's one more option, you can process all elements of this collection using a loop as:

    For Each control1 In TableLayoutPanel1.Controls
    'You can manipulate control1
    'More often than not, controls in a single column of the TableLayoutPanel are
    'of the same type.
    'So, you can again use TypeOf & Is Operator to know the type of control
    'and manipulate it appropriately.
    Next
    Monday, April 23, 2007 5:00 PM

All replies

  • Anybody with an answer?????
    Wednesday, November 22, 2006 8:59 AM
  • Hi Rahyl,

    there is only one way

    Control c = TableLayoutPanel1.GetControlFromPosition(0,0)

    it returns the control which is at 0,0 position means at first position

    and if u have any  good links for C# window forms then tell me
    Monday, April 23, 2007 10:03 AM
  • sorry its Rahul my mistake
    Monday, April 23, 2007 10:16 AM
  • Well, it's been quite sometime since I made this post. I could not find a direct solution to my problem, but there are workarounds you can use.

    Like, you can construct an array of child controls for the particular column or row of the TableLayoutPanel (you would be able to do this, only if you know the name of controls or their index in the TableLayoutPanel). You can then use this array in any way you want. Remember, before accessing any property that does not belong to the Control class itself on a element of this array, use TypeOf & Is opertor (in VB.NET) to make sure you are not calling an invalid property on a control (unless offcourse you are sure that you are calling a valid property on the element).

    For my purpose, I found a workaround using Controls collection of the TableLayoutPanel. You can use this collection to index the controls either by their name, or by their position in the collection.

    There's one more option, you can process all elements of this collection using a loop as:

    For Each control1 In TableLayoutPanel1.Controls
    'You can manipulate control1
    'More often than not, controls in a single column of the TableLayoutPanel are
    'of the same type.
    'So, you can again use TypeOf & Is Operator to know the type of control
    'and manipulate it appropriately.
    Next
    Monday, April 23, 2007 5:00 PM
  • Here's how to hide a row/Column.  I'm totally sure how this works if your row or column spans other rows/columns, but I got what I needed by experimenting.

    TableLayoutPanelGraph.GetControlFromPosition(colIndex, rowIndex).Visible = False

    Has anyone figured out how to set the width of a column at runtime?

    Saturday, September 15, 2007 6:35 PM
  • Simply set the width for the corresponding ColumnStyle. Here's how you can do it:

    Code Snippet
                Me.TableLayoutPanel1.ColumnStyles(0).Width = 10.0

    Sunday, September 16, 2007 5:07 AM
  • Hi, it's a long time, but I actually work on something simillar and here is the code for adding the controls to specified col, row

    TableLayoutPanel somePanel = new TableLayoutPanel(); //here is the panel to use
    
    TableLayoutControlCollection c = new TableLayoutControlCollection(somePanel);
    c.Add(button1, 0, 1);
    

    you can use TableLayoutControlCollection  for other manipulations

    P.S, how to set text color in the forum :)

     

     

    Wednesday, November 10, 2010 10:54 PM
  • Hi, it's a long time, but I actually work on something simillar and here is the code for adding the controls to specified col, row

    TableLayoutPanel somePanel = new TableLayoutPanel(); //here is the panel to use
    
    
    
    TableLayoutControlCollection c = new TableLayoutControlCollection(somePanel);
    
    c.Add(button1, 0, 1);
    
    
    
    

    you can use TableLayoutControlCollection  for other manipulations

    P.S, how to set text color in the forum :)

     

     

     

     


    Actuallly it doesnt work :)

    to add control use just somePanel.Controls.Add(Button1,0,1);

     

    re: "I want to know is that how can i make it

    TableLayoutPanel1.Columns(0).Controls"

    No, because each cell contains only one control, you must access by Cell, Row index

    Wednesday, November 10, 2010 11:06 PM