2014-01-11 92 views
-1

我在面板中動態添加了複選框,並且在執行後,如何獲得單擊按鈕時選中哪個複選框。獲取動態添加的複選框的檢查狀態

public int x=550 ,y=10; 
    private void Form1_Load(object sender, EventArgs e) 
    { 

     var panel1 = new Panel() 
     { 
      Size = new Size(600, 70), 
      Location = new Point(20, 130), 
      BorderStyle = BorderStyle.FixedSingle 
     }; 
     for (int i = 0; i < 20; i++) 
     { 
      CheckBox chkpremiumtickets = new CheckBox(); 
      chkpremiumtickets.Text = " "; 
      chkpremiumtickets.Name = "chkboxpremiumtickets"; 
      chkpremiumtickets.Location = new Point(x,y); 
      panel1.Controls.Add(chkpremiumtickets); 
      x = x - 55; 
      if (x < 55) 
      { 
       y = y+20; 
       x = 550; 
      } 
     } 

     x = 550; y = 10; 

     Controls.Add(panel1); 

回答

0
foreach (Control control in panel1.Controls) 
{ 
    if (control is CheckBox) 
    { 
    if ((control as CheckBox).Checked) 
    { 
     ... 
    } 
    } 
} 

,或者乾脆利用LINQ,你可以得到所有選中的面板內CheckBox ES:

var checkedItems = panel1.Controls.OfType<CheckBox>().Where(c => c.Checked); 
1

我怎樣才能得到它的複選框被選中?
您可以使用CheckBox(s)的Checked財產作爲此用途。

private void button1_click(object sender, EventArgs e) 
    { 
     if(checkBox1.Checked == true) 
      // do something 
     else if (checkBox2.Checked == true) 
      // do something else. 
} 

對於動態控件,您可以使用: -

foreach(Control ctrl in panel1.Controls) 
    { 
     if(ctrl is CheckBox) 
     { 
      CheckBox tempCheckBox = ctrl as CheckBox ; 
      if(tempCheckBox.Checked == true) 
       // do something. 
     } 
    } 

您也可以使用Tag財產,如果你要區分/黑白不同的CheckBox(S)。
使用LINQ

Func<Control, bool> checkedPredicate = (c) => { 
             if((c is CheckBox)&&(c.Checked == true)) 
              return true ; 
             return false ; 
            } ; 
var checkedList = panel1.Controls.Where(x => checkedPredicate(x)).ToArray() ; 
// this is a array of checkboxes. 
+0

他的控件是動態的,所以不會幫助! –

0

遍歷面板中的每個控制和檢查特定類型的控制,併爲您與它相關的對象檢查它的標籤屬性!下面

例正常工作:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication3 
{ 
    public partial class Form1 : Form 
    { 
     public int x = 550, y = 10; 
     public Form1() 
     { 
      InitializeComponent(); 

      var panel1 = new Panel() 
      { 
       Size = new Size(600, 600), 
       Location = new Point(20, 130), 
       BorderStyle = BorderStyle.FixedSingle, 
       Name = "tktPanel", 
      }; 
      for (int i = 0; i < 20; i++) 
      { 
       CheckBox chkpremiumtickets = new CheckBox(); 
       chkpremiumtickets.Text = " "; 
       chkpremiumtickets.Name = "chk-" + i.ToString(); 
       chkpremiumtickets.Location = new Point(x, y); 
       panel1.Controls.Add(chkpremiumtickets); 
       chkpremiumtickets.Visible = true; 
       x = x - 55; 
       if (x < 55) 
       { 
        y = y + 20; 
        x = 550; 
       } 
      } 

      x = 550; 
      y = 10; 

      Controls.Add(panel1); 
      panel1.Visible = true; 

     } 

     private IEnumerable<string> GetSelectedBtn() 
     { 
      //get selected chk_box 
      var panel = (Panel)this.Controls["tktPanel"]; 

      foreach (var control in panel.Controls) 
      { 
       var chk = control as CheckBox; 
       if (chk != null && chk.Checked) 
       { 
        yield return chk.Name; 
       } 
      } 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      foreach (var chkName in GetSelectedBtn()) 
      { 
       MessageBox.Show(chkName); 
      } 
     } 
    } 
} 
0

你可以使用一個集合,以保持該複選框的引用,然後用收集來操縱例如複選框

收集部件添加到窗體

private List<CheckBox> dynamicCheckboxes = null; 

添加複選框集合在同一時間將它們添加到面板

panel1.Controls.Add(chkpremiumtickets); 
this.dynamicCheckboxes.Add(chkpremiumtickets); 

然後,當你想檢查哪些複選框被檢查

var checkedPremiumTickets = this.dynamicCheckboxes.Where(c => c.Checked); 

這也解除了從表單連接複選框狀態檢查他們碰巧屬於他們的食物。雖然你可能不在乎這一點。