Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if dynamic checkbox are checked

I’ve an application to import some pictures from a folder into another. I use picturebox to show my pictures and have a checkbox next to it. If it’s unchecked i dont wanna import them.

So here is my code for creating a checkbox :

        public void CreateCheckBox(Form formInstance,int yLocation, int xLocation, int iNumber)
        {
            CheckBox box = new CheckBox();
            box.Name = "cbxName" + iNumber.ToString();
            box.Location = new Point(xLocation+40,yLocation+90);
            box.Visible = true;
            box.Enabled = true;
            box.Checked = true;
            box.CheckedChanged += new EventHandler(cbx_CheckedChange);
            formInstance.Controls.Add(box);
        }

And my pictureBox :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 public void CreatePictureBox(Form formInstance,int iNumber)
        {
            string[] tNomImage = RecupererNomImage();
            PictureBox pbxImage = new PictureBox();
            pbxImage.Name = "pbxName" + iNumber.ToString();
            pbxImage.Image = Image.FromFile(tNomImage[iNumber]);
            pbxImage.Width = 90;
            pbxImage.Height = 90;
            pbxImage.Location = new Point(iXLocation, iYLocation);
            pbxImage.Visible = true;
            pbxImage.BorderStyle = BorderStyle.FixedSingle;
            pbxImage.SizeMode = PictureBoxSizeMode.Zoom; 
            formInstance.Controls.Add(pbxImage);
            pbxImage.Enabled = false;
            CreateCheckBox(this, iYLocation, iXLocation, iNumber);
            if (iXLocation+iXSpacing*2 > this.Width)
            {
                iXLocation = XLOCATION;
                iYLocation += iXSpacing;
            }
            else
            {
                iXLocation += iXSpacing;
            }

And I wanna know which checkbox is checked so I can export the picture next to it.

>Solution :

I presume a picturebox will have a name like:

pbxName141

And its matching checkbox will have a name like:

cbxName141

You can ask the form for all the checked boxes:

var cbs = formInstance.Controls.OfType<CheckBox>().Where(cb => cb.Checked);

You can convert the checkbox name into the related picturebox name and look up the picturebox by name:

foreach(var cb in cbs){
  var pbName = "p" + cb.Name.Substring(1);
  var pb = formInstance.Controls[pbName];
}

So many ways to skin this cat..

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading