1

我根据表中的记录数动态地在 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();
        }
4

2 回答 2

1

假设你在询问 WinForm 每个控件中有一个标签属性,你可以利用它。

 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();
         }

    }
于 2013-04-02T11:32:08.207 回答
1

您可以将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();
    }
}
于 2013-04-02T11:34:19.843 回答