Search code examples
c#checkboxuser-controlsflowlayoutpanel

get label text of all checked checkbox in user control inside flowlayoutpanel


I'm working on a Restaurant POS system, I created a form to show all Extras that users can ask to add to there orders, here is the form :

enter image description here

Inside this form, I added a flowlayoutpanel and I added multiple user controls to this flowlayoutpanel, each user control have a PictureBox and Label and CheckBox. I'm filling the user controls in flowlayoutpanel like this :

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

and I'm able to get label text like this:

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

I'm able to get the label text for clicked user control, What I want now is how to get all label texts for checked checkBox??


Solution

  • According to the code, you have a UserControl named Extra_uc that contains the Extras_name property which holds the name of an extra item, and a CheckBox control which its Modifiers property is set to internal/public.

    If that's the case, you can get the checked items as simple as this:

    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 ?