[英]Fetch value from dynamically generated Panel Controls
我根據表中的記錄數動態地在flowlayoutpanel中添加面板控件。 我想從每個面板中獲取ID,以便我可以在面板的單擊事件上打開一個彈出窗口。 有任何建議嗎?
這是我的代碼示例。
int id=0;
public void FillSearch()
{
var playerList = dianaDB.TblPlayers.Where(p => p.Status == "Active" & p.CancellationStatus == "Active" & p.Version == "Active").Select(p => p);
Panel pnlPlayer = new Panel();
foreach (var pl in playerList)
{
pnlPlayer = new Panel();
pnlPlayer.Size = new Size(153, 116);
pnlPlayer.BorderStyle = BorderStyle.FixedSingle;
pnlPlayer.Cursor = Cursors.Hand;
pnlPlayer.Click += new EventHandler(pbx_Click);
id=pl.Id;
}
}
private void pbx_Click(object sender, EventArgs e)
{
DlgSearchDetails newDlg = new DlgSearchDetails(id);
newDlg.ShowDialog();
}
假設您正在詢問WinForm,每個控件中都有一個tag屬性,ypou可以利用它。
public void FillSearch()
{
var playerList = dianaDB.TblPlayers.Where(p => p.Status == "Active" & p.CancellationStatus == "Active" & p.Version == "Active").Select(p => p);
Panel pnlPlayer = new Panel();
foreach (var pl in playerList)
{
pnlPlayer = new Panel();
pnlPlayer.Size = new Size(153, 116);
pnlPlayer.BorderStyle = BorderStyle.FixedSingle;
pnlPlayer.Cursor = Cursors.Hand;
pnlPlayer.Click += new EventHandler(pbx_Click);
pnlPlayer.Tag = pl.Id;
}
}
private void pbx_Click(object sender, EventArgs e)
{
var panle = sender as Panel;
if(panel!=null)
{
DlgSearchDetails newDlg = new DlgSearchDetails(panel.Tag);
newDlg.ShowDialog();
}
}
您可以將面板的ID
存儲在其Tag屬性中。
pnlPlayer.Tag = id;
然后稍后取回
private void pbx_Click(object sender, EventArgs e)
{
Panel p = sender as Panel;
if(p != null)
{
//TODO add error handling to ensure Tag contains an int
//...
DlgSearchDetails newDlg = new DlgSearchDetails((int)p.Tag);
newDlg.ShowDialog();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.