2013-04-02 16 views
1

根據我表中的記錄數動態地在面板控制面板中添加面板控件。我想從每個面板獲得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(); 
     } 

回答

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

thanku thanku .. :) :)感謝名單了很多..救了我的一天。驚人的解決方案.. –

1

假設,你是問關於在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(); 
     } 

    } 
+0

感謝名單感謝名單@Saurabh ..它的工作... :) :) –