为“流布局”面板指定标签
本文关键字:流布局 标签 | 更新日期: 2023-09-27 18:36:10
嗨,我试图根据cell
的Value
为FlowLayoutPanel
分配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.Find
在Form
中查找特定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);
}
}