[英]How to find index of controls in flowlayoutpanel in C#
I am new to C#. 我是C#的新手。
I have Form1
and flowlayoutpanel
. 我有
Form1
和flowlayoutpanel
。 I dynamically add Buttons
to flowlayoutpanel
and the buttons
details comes from a database
table
. 我将
Buttons
动态添加到flowlayoutpanel
,而buttons
详细信息来自database
table
。
I want to know the name of the first button
in the flowlayoutpanel
. 我想知道
flowlayoutpanel
第一个button
的名称。
for (i = 0; i < DataTable.Rows.Count; i++)
{
Button btn = new Button();
btn.Name = DataTable.Rows[i]["Name"].ToString();
btn.Text = DataTable.Rows[i]["PostCode"].ToString();
flowlayoutpanel.Controls.Add(btn);
}
String First_Button_Name = ...........
如果要获取要添加到FlowLayoutPanel的第一个按钮的名称,无论这些按钮如何到达那里,请使用:
string firstButtonName = flowlayoutpanel.Controls.OfType<Button>().FirstOrDefault()?.Name;
This is the value you are searching for: 这是您要搜索的值:
DataTable.Rows[0]["Name"].ToString()
but make sure you have at least an element before you try to get this value. 但在尝试获取此值之前,请确保至少有一个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.