简体   繁体   中英

How to find index of controls in flowlayoutpanel in C#

I am new to C#.

I have Form1 and flowlayoutpanel . I dynamically add Buttons to flowlayoutpanel and the buttons details comes from a database table .

I want to know the name of the first button in the flowlayoutpanel .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM