获取flowlayoutpanel内用户控件中所有选中复选框的标签文本
get label text of all checked checkbox in user control inside flowlayoutpanel
我在餐厅 POS 系统上工作,我创建了一个表格来显示用户可以要求添加到那里的订单的所有额外服务,这是表格:
在此表单中,我添加了一个 flowlayoutpanel
并向此 flowlayoutpanel
添加了多个用户控件,每个用户控件都有一个 PictureBox
和 Label
和 CheckBox
。我正在像这样在 flowlayoutpanel
中填充用户控件:
flowLayoutPanel2.Controls.Clear();
DB_conn.Query2("get_food_extras");
DB_conn._dr = DB_conn._cmd.ExecuteReader();
while (DB_conn._dr.Read())
{
Extra_uc UC = new Extra_uc
{
Extras_ID = DB_conn._dr["ID"].ToString(),
Extras_Price = Convert.ToDecimal(DB_conn._dr["price"].ToString()),
Extras_name = DB_conn._dr["name"].ToString()
};
if (DB_conn._dr["image"] != DBNull.Value)
{
byte[] image = (byte[])DB_conn._dr["image"];
image = Decompress(image);
var stream = new MemoryStream();
stream.Write(image, 0, image.Length);
var bitmap = new Bitmap(stream);
UC.Extras_image = bitmap;
}
UC.CheckBox1.CheckedChanged += UC_PictureBox1_Click;
UC.MaterialImage.Click += UC_PictureBox1_Click;
UC.Material_name_txt.Click += UC_PictureBox1_Click;
UC.MaterialImage.MouseDown += UC_PictureBox1_MouseDown;
UC.Material_name_txt.MouseDown += UC_PictureBox1_MouseDown;
UC.MaterialImage.MouseMove += UC_PictureBox1_MouseMove;
UC.Material_name_txt.MouseMove += UC_PictureBox1_MouseMove;
flowLayoutPanel2.Controls.Add(UC);
}
DB_conn._dr.Close();
而且我能够得到这样的标签文本:
private void UC_PictureBox1_Click(object sender, EventArgs e)
{
if (!Food_Moved)
{
string Extra_Name = "";
CheckState checkd = CheckState.Unchecked;
if (sender is Label lab)
{
Extra_Name = lab.Tag.ToString();
}
else if (sender is PictureBox btn1)
{
Extra_Name = btn1.Tag.ToString();
}
else if (sender is Guna.UI2.WinForms.Guna2CheckBox ck)
{
checkd = ck.CheckState;
}
MessageBox.Show(Extra_Name.ToString());
}
else
{
Food_Moved = false;
}
}
我可以获取点击user control
的label
文本,我现在想要的是如何获取选中checkBox
的所有label
文本?
根据代码,您有一个名为 Extra_uc
的 UserControl
,其中包含 Extras_name
属性,其中包含一个额外项目的名称,以及一个 CheckBox
控件,其 Modifiers
属性 设置为 internal/public
.
如果是这样,您可以像这样简单地获取已检查的项目:
private IEnumerable<string> GetCheckedNames() =>
flowLayoutPanel2.Controls.OfType<Extra_uc>()
.Where(uc => uc.CheckBox1.Checked)
.Select(uc => uc.Extras_name); // Or maybe uc.Material_name_txt.Text ?
我在餐厅 POS 系统上工作,我创建了一个表格来显示用户可以要求添加到那里的订单的所有额外服务,这是表格:
在此表单中,我添加了一个 flowlayoutpanel
并向此 flowlayoutpanel
添加了多个用户控件,每个用户控件都有一个 PictureBox
和 Label
和 CheckBox
。我正在像这样在 flowlayoutpanel
中填充用户控件:
flowLayoutPanel2.Controls.Clear();
DB_conn.Query2("get_food_extras");
DB_conn._dr = DB_conn._cmd.ExecuteReader();
while (DB_conn._dr.Read())
{
Extra_uc UC = new Extra_uc
{
Extras_ID = DB_conn._dr["ID"].ToString(),
Extras_Price = Convert.ToDecimal(DB_conn._dr["price"].ToString()),
Extras_name = DB_conn._dr["name"].ToString()
};
if (DB_conn._dr["image"] != DBNull.Value)
{
byte[] image = (byte[])DB_conn._dr["image"];
image = Decompress(image);
var stream = new MemoryStream();
stream.Write(image, 0, image.Length);
var bitmap = new Bitmap(stream);
UC.Extras_image = bitmap;
}
UC.CheckBox1.CheckedChanged += UC_PictureBox1_Click;
UC.MaterialImage.Click += UC_PictureBox1_Click;
UC.Material_name_txt.Click += UC_PictureBox1_Click;
UC.MaterialImage.MouseDown += UC_PictureBox1_MouseDown;
UC.Material_name_txt.MouseDown += UC_PictureBox1_MouseDown;
UC.MaterialImage.MouseMove += UC_PictureBox1_MouseMove;
UC.Material_name_txt.MouseMove += UC_PictureBox1_MouseMove;
flowLayoutPanel2.Controls.Add(UC);
}
DB_conn._dr.Close();
而且我能够得到这样的标签文本:
private void UC_PictureBox1_Click(object sender, EventArgs e)
{
if (!Food_Moved)
{
string Extra_Name = "";
CheckState checkd = CheckState.Unchecked;
if (sender is Label lab)
{
Extra_Name = lab.Tag.ToString();
}
else if (sender is PictureBox btn1)
{
Extra_Name = btn1.Tag.ToString();
}
else if (sender is Guna.UI2.WinForms.Guna2CheckBox ck)
{
checkd = ck.CheckState;
}
MessageBox.Show(Extra_Name.ToString());
}
else
{
Food_Moved = false;
}
}
我可以获取点击user control
的label
文本,我现在想要的是如何获取选中checkBox
的所有label
文本?
根据代码,您有一个名为 Extra_uc
的 UserControl
,其中包含 Extras_name
属性,其中包含一个额外项目的名称,以及一个 CheckBox
控件,其 Modifiers
属性 设置为 internal/public
.
如果是这样,您可以像这样简单地获取已检查的项目:
private IEnumerable<string> GetCheckedNames() =>
flowLayoutPanel2.Controls.OfType<Extra_uc>()
.Where(uc => uc.CheckBox1.Checked)
.Select(uc => uc.Extras_name); // Or maybe uc.Material_name_txt.Text ?