我需要在将控件添加到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
}
答案 0 :(得分:2)
如果您向RowCount/ColumnCount
添加新RowStyles/ColumnStyles
,则需要增加TableLayoutPanel
。在此之后,您可以轻松使用GetPositionFromControl(Control value)
方法。
我认为,ControlAdded
在这种情况下,事件是更好的方法。试一试。
这样做:
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();
}
}
答案 1 :(得分:0)
在离开邮件后,尝试按下button__Click东西(Windows messagequeue message):
SynchronizationContext.Current.Post(new SendOrPostCallback(delegate(object state)
{
//Your code here...
}), true);
答案 2 :(得分:0)
控件的行位置,如果控件的位置是-1,则为-1 由LayoutEngine确定。
使用以下内容:
public void ControlAdded(object sender, EventArgs e)
{
var row = layout.GetPositionFromControl(lbl);//0,0
}