3

当控件添加到 Windows 窗体中的 TableLayoutPanel 时,我需要采取措施。我正在处理控件的 ParentChanged 事件以查明控件是否已添加到父级(此处为 TableLayoutPanel),但我收到的索引是-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 ??????
}

我需要添加控件的行号。控件应采取独立于何时添加到 TableLayoutPanel 的操作。如何得到它?

    void t_ControlAdded(object sender, ControlEventArgs e)
    {
        int row = t.GetRow(e.Control);      //this also gives row = -1      
    }
4

3 回答 3

3

如果您RowCount/ColumnCount将新添加RowStyles/ColumnStyles到您的TableLayoutPanel. 在此之后,您可以轻松地使用该GetPositionFromControl(Control value)方法。

我认为,ControlAdded在这种情况下,Event 是更好的方法。试试看。

像这样做:

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();
    }
}
于 2012-10-09T19:28:44.110 回答
0

留下消息后尝试做 button1_Click 的东西(windows messagequeue 消息):

SynchronizationContext.Current.Post(new SendOrPostCallback(delegate(object state)
{
  //Your code here...
}), true);
于 2012-10-09T13:13:21.353 回答
0

msdn 页面

控件的行位置,如果控件的位置由 LayoutEngine 确定,则为 -1。

使用以下内容:

public void ControlAdded(object sender, EventArgs e)
{
    var row = layout.GetPositionFromControl(lbl);//0,0
}
于 2012-10-09T13:36:59.143 回答