为“流布局”面板指定标签

本文关键字:流布局 标签 | 更新日期: 2023-09-27 18:36:10

嗨,我试图根据cellValueFlowLayoutPanel分配label我的代码:

foreach (DataGridViewRow row in datagridview.SelectedRows)
{
    var Id = row.Cells["typeID"].Value.ToString();
    int typeID;
    if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out PlayerID))
    {
        Label label1 = new Label();
        label1.Text = row.Cells["PlayerType"].Value.ToString();
      //Does not matter for type of player
      //flpGoalie.Controls.Add(label1);
      if(label1.Text == "Goalie")
      {
          flpGoalie.Controls.Add(label1);
      }
 }

此行flpGoalie.Controls.Add(label1); ,将任何label分配给该FlowLayoutPanel

我试图做的是根据cell的值将PlayerType分离成不同的FLP

为“流布局”面板指定标签

由于FLP名称和label1中的Text之间存在模式,因此您可以使用Controls.FindForm中查找特定Control,如下所示:

foreach (DataGridViewRow row in datagridview.Rows.Cast<DataGridViewRow>())
{
    var Id = row.Cells["typeID"].Value.ToString();
    int typeID;
    if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out PlayerID))
    {
        Label label1 = new Label();
        label1.Text = row.Cells["PlayerType"].Value.ToString();
      //Does not matter for type of player
      //flpGoalie.Controls.Add(label1);
    Control[] ctrls = Controls.Find("flp" + label1.Text, true);
    if (ctrls != null && ctrls.Length > 0){
        FlowLayoutPanel flp = ctrls[0] as FlowLayoutPanel;
        if(flp != null)
            flp.Controls.Add(label1);
    }
}