Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8305435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:06:57+00:00 2026-06-08T18:06:57+00:00

I am creating a few checkboxes when I open a form with the following

  • 0

I am creating a few checkboxes when I open a form with the following code:

    private void OpenFolder_Load(object sender, EventArgs e)
    {
        int i = 0;
        foreach (string file in filesToOpen)
        {
            Label lbl = new Label();
            lbl.Text = Path.GetFileNameWithoutExtension(file);
            lbl.Width = 200;
            lbl.Height = 25;
            lbl.AutoEllipsis = true;
            lbl.Location = new System.Drawing.Point(10, 40 + 25 * i);

            this.Controls.Add(lbl);

            string checkName = "check" + i;
            CheckBox check = new CheckBox();
            check.Checked = true;
            check.AccessibleName = checkName;
            check.Location = new System.Drawing.Point(340, 40 + 25 * i);
            check.CheckedChanged +=new EventHandler(check_CheckedChanged);

            this.Controls.Add(check);

            CheckBoxes.Add(check);

            i++;
        }

and I am trying to check the state of the checkboxes everytime one changes to toggle my OK button (the user can validate only if there are a certain number of the checkboxes checked)

here is the code I use, but it fails as I am not able to target the checkboxes:

    private void check_CheckedChanged(Object sender, EventArgs e)
    {
        for (int i = 0; i < filesToOpen.Count(); i++)
        {
            string tbarName = "tbar" + i;
            string checkName = "check" + i;

            CheckBox ckb = this.Controls.OfType<CheckBox>()
                     .Where(c => c.AccessibleName.Equals(checkName)) as CheckBox;
            TrackBar tkb = this.Controls.OfType<TrackBar>()
                     .Where(t => t.AccessibleName.Equals(tbarName)) as TrackBar;
            //TrackBar tkb = this.Controls.Find(tbarName, false).First() as TrackBar;
            //CheckBox ckb = this.Controls.Find(checkName, false).First() as CheckBox;

            if (ckb.Checked == true)
            {
                //do stuff
            }
        }
    }

what am I doing wrong/really wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:06:58+00:00Added an answer on June 8, 2026 at 6:06 pm

    Given that you add the checkboxes to your own list:

    CheckBoxes.Add(check);
    

    it would be simpler to loop over that rather than trying to find the control associated with the file:

    foreach (var checkBox in CheckBoxes)
    {
        if (checkbox.Checked)
        {
            // Do stuff...
        }
    }
    

    However, you shouldn’t need to use a separate list. This line is wrong:

    CheckBox ckb = this.Controls.OfType<CheckBox>()
                       .Where(c => c.AccessibleName.Equals(checkName)) as CheckBox;
    

    Where returns a IEnumerable<CheckBox> but you are trying to cast it directly to a CheckBox which will return null. What you should have is:

    CheckBox ckb = this.Controls.OfType<CheckBox>()
                       .Where(c => c.AccessibleName.Equals(checkName)).First();
    

    You will still need to check to see if ckb is null (just in case there is nothing on the list) but this should return you the control you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a few pictureboxes dynamically, then assigning the following: // class variable
i am creating a few input fields in a foreach loop: <?php foreach($this->results as
I am following few tutorials for creating MVC3 application. I saw example of adding
I am creating few pages (including login page) which should only be accessible by
i am creating a few divs from some JSONP data that i am retrieving
I'm creating quite a few SSIS packages to drive document composition (e.g. utility bills,
Hello I'm creating a game and I have a few ways to make trainers
I am creating a web application which has a few management screens, where a
I am creating small django application which holds some few questions (and answers for
I've been experimenting with creating a canvas element in a few different ways and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.