gpt4 book ai didi

c# - 查找添加到 TableLayoutPanel 的控件的行索引

转载 作者:行者123 更新时间:2023-11-30 17:09:37 26 4
gpt4 key购买 nike

当控件添加到 Windows 窗体中的 TableLayoutPanel 时,我需要执行操作。我正在处理控件的 ParentChanged 事件以查明控件是否已添加到父级(此处为 TableLayoutPanel),但我收到的索引是 -1。

表格布局面板;

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
}

最佳答案

如果您向 TableLayoutPanel 添加新的 RowStyles/ColumnStyles,则需要增加 RowCount/ColumnCount。在此之后,您可以轻松地使用 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();
}
}

关于c# - 查找添加到 TableLayoutPanel 的控件的行索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12800520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com