我在面板中动态添加了复选框,执行后如何在单击按钮时选中哪些复选框。
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 :(得分: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.
}
}
如果您想区分不同的CheckBox
(s),您还可以使用Tag属性。
使用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.
答案 1 :(得分:0)
foreach (Control control in panel1.Controls)
{
if (control is CheckBox)
{
if ((control as CheckBox).Checked)
{
...
}
}
}
或者只是使用LINQ,您可以在面板中获得所有选中的CheckBox
:
var checkedItems = panel1.Controls.OfType<CheckBox>().Where(c => c.Checked);
答案 2 :(得分:0)
迭代Panel中的每个控件并检查特定类型的控件,并检查与其关联的对象的Tag属性!
以下示例正常工作:
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);
}
}
}
}
答案 3 :(得分:0)
您可以使用集合来保留对复选框的引用,然后使用该集合来操作复选框,例如。
将一个集合成员添加到表单
private List<CheckBox> dynamicCheckboxes = null;
将复选框添加到集合中,同时将它们添加到面板
panel1.Controls.Add(chkpremiumtickets);
this.dynamicCheckboxes.Add(chkpremiumtickets);
然后,当您想要检查选中哪个复选框时
var checkedPremiumTickets = this.dynamicCheckboxes.Where(c => c.Checked);
这也将复选框状态检查与它们碰巧所属的Form容器分离。虽然你可能不关心这个。