Find row index of control added to TableLayoutPanel

13,487

You need to increment RowCount/ColumnCount, if you add new RowStyles/ColumnStyles to your TableLayoutPanel. After this you can easily use the GetPositionFromControl(Control value) Method.

I think, ControlAdded Event is the better approach in this case. Give it a try.

Do it like this:

public partial class Form1 : Form
{
    private TableLayoutPanel panel;

    public Form1()
    {
        InitializeComponent();
        InitializeTableLayoutPanel();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        AddControl(0, 0);
        AddControl(0, 1);
        AddControl(1, 0);
        AddControl(1, 1);
        AddControl(2, 0);
        AddControl(2, 1);
        AddControl(3, 0);
        AddControl(3, 1);
    }

    private void InitializeTableLayoutPanel()
    {
        panel = new TableLayoutPanel();
        panel.Dock = DockStyle.Fill;
        panel.AutoSize = true;
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
        panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
        panel.ColumnCount = 2;
        panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        panel.RowCount = 2;

        panel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
        panel.ControlAdded += new ControlEventHandler(OnControlAdded);

        this.Controls.Add(panel);
    }

    private void OnControlAdded(object sender, ControlEventArgs e)
    {
        if (e.Control != null)
        {
            int column = panel.GetPositionFromControl(e.Control).Column;
            int row = panel.GetPositionFromControl(e.Control).Row;

            MessageBox.Show(string.Format("Column: {0}, Row: {1}", column, row));
        }
    }

    /// <summary>
    /// Add Control to Panel
    /// </summary>
    /// <param name="column">column position</param>
    /// <param name="row">row position</param>
    private void AddControl(int column, int row)
    {
        Label label = new Label();
        label.Font = new Font(new FontFamily("Droid Sans"), 20, FontStyle.Bold);
        label.Name = "label";
        label.Text = "Whoop!";

        if (column < panel.ColumnCount && row < panel.RowCount)
            panel.Controls.Add(label, column, row);
        else
            throw new ArgumentOutOfRangeException();
    }
}
Share:
13,487
Brij
Author by

Brij

software developer :)

Updated on September 03, 2022

Comments

  • Brij
    Brij over 1 year

    I need to take an action when a control is added to a TableLayoutPanel in Windows Forms. I am handling ParentChanged event of the control to find out if the control is added to a parent(here TableLayoutPanel), but the index I receive is -1.

    TableLayoutPanel t;

    private void button1_Click(object sender, EventArgs e)
    {
        // this.Text = tableLayoutPanel1.Height.ToString();
    
        t = new TableLayoutPanel();
        t.Dock = DockStyle.Fill;
        //t.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        t.AutoSize = true;
        //t.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
        t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
        panel1.Controls.Add(t);
    
        t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
    
    
        Label lbl = new Label();
        lbl.ParentChanged += new EventHandler(lbl_ParentChanged);
        lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20);
        lbl.Text = "Hello";
        t.Controls.Add(lbl, 0, 0);
    }
    
    void lbl_ParentChanged(object sender, EventArgs e)
    {
        Label lbl = (Label)sender;
        int row = t.GetRow(lbl);//here I get row = -1 ??????
    }
    

    I need row number to which the control is added. The control should take action independent of when it is added to the TableLayoutPanel. How to get it ?

        void t_ControlAdded(object sender, ControlEventArgs e)
        {
            int row = t.GetRow(e.Control);      //this also gives row = -1      
        }
    
  • Brij
    Brij over 11 years
    Controls are getting added to the TableLayoutPanel from various sources. button1_click is just a sample code I have written.
  • Brij
    Brij over 11 years
    layout.GetPositionFromControl(lbl) always gives (0,0) always, even if the control is added in a different row/column index.
  • Carra
    Carra over 11 years
    I did some more testing. It works if you use it after an addition (layout.controls.add(x,0,0) > layout.GetPositionFromControl(x), not in the controladded event.
  • Brij
    Brij over 11 years
    So you are calling layout.controls.add(x,0,0), then calling layout.GetPositionFromControl(x). But in my case X is a control I want it to react the moment it is added to a parent(here TableLayoutPanel). So I was using ParentChanged of X.
  • Peter Larsen 'CPH'
    Peter Larsen 'CPH' over 11 years
    Well, the idea is that you may get unpredictable result if an event trigger an even - as seen in lots of situations, right!!
  • Brij
    Brij over 11 years
    After setting RowCount/ColumnCount, I am able to get the row/column index in ControlAdded event. Still it doesn't work for the control which is added in its ParentChanged event.