将标签分配给FlowLayoutPanel

时间:2016-02-24 13:20:25

标签: c# winforms

您好我正在尝试根据label的{​​{1}}向FlowLayoutPanel分配Value 我的代码:

cell

此行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的值将PlayerType分隔到不同的FLP

1 个答案:

答案 0 :(得分:1)

由于FLP名称与Text中的label1之间存在模式,因此您可以使用Controls.Find在您的Control中找到特定的Form 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); } } 喜欢这样:

{{1}}