0

I am making a DTO creator. I am able get the table names and put them into datatable. The problem is, I must create checkboxes dynamically using these table names, and then be able to get whatever items checked. This is what I have been able to come up with so far:

for (int i = 0; i < dtable.Rows.Count; i++)
        {
            string cbName = dtable.Rows[i][0].ToString();
            //Console.WriteLine(dtable.Rows[i][0]);
            CheckBox box = new CheckBox();
            box.Tag = i.ToString();
            box.Text = cbName;
            box.AutoSize = true;
            box.Location = new Point(10, i * 50); //vertical
            //box.Location = new Point(i * 50, 10); //horizontal
            this.Controls.Add(box);
        }

The dtable already has names and I create the checkboxes. However they are out the rendered area of the form, can see at most 10 of them. Also, how can I register which boxes are checked during runtime?

5
  • When you say "out of bounds", do you mean they are being rendered outside of the form's visible area? Commented Mar 9, 2016 at 12:04
  • yes, they are, let me correct that part Commented Mar 9, 2016 at 12:05
  • Why not just add a scroll bar? Or use a FlowLayoutPanel instead of absolute positioning? Commented Mar 9, 2016 at 12:09
  • yeah, that helped but the main issue is, i need to register them somewhere once clicked, how will i differ them from each other? Commented Mar 9, 2016 at 12:24
  • Add an event handler to each one in your loop? Iterate through the collection of Controls and get each value? Many ways that can be achieved. Commented Mar 9, 2016 at 12:26

1 Answer 1

1

You can either put your CheckBoxes in a List and then count the checked ones.

List<CheckBox> lstBoxes = new List<CheckBox>();

// create box
...

lstBoxes.Add(box);

// Checking for checked boxes (eg. on form exit)
var checkedBoxes = lstBoxes.Where(b => b.Checked);

Or when you're creating your checkboxes, add an event on checked changed :

box.CheckedChanged += (sender, e) =>
        {
            var senderAsBox = sender as CheckBox;

            if (senderAsBox == null) return;

            var state = senderAsBox.Checked;

            // Do you stuff then...
        };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.