Find row index of control added to TableLayoutPane

2020-04-11 10:30发布

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      
    }

3条回答
迷人小祖宗
2楼-- · 2020-04-11 11:10

Try do your button1_Click stuff after you have left the message (windows messagequeue message):

SynchronizationContext.Current.Post(new SendOrPostCallback(delegate(object state)
{
  //Your code here...
}), true);
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-04-11 11:17

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();
    }
}
查看更多
兄弟一词,经得起流年.
4楼-- · 2020-04-11 11:32

From the msdn page:

The row position of control, or -1 if the position of control is determined by LayoutEngine.

Use the following:

public void ControlAdded(object sender, EventArgs e)
{
    var row = layout.GetPositionFromControl(lbl);//0,0
}
查看更多
登录 后发表回答