0

In C#, I'm trying to dynamically add checkboxes to a tab sheet on a form. I've tried the AutoSize property but some of my text is too long. the x.Size.Width is returning:

Cannot modify the return value of 'System.Windows.Froms.Control.Size' because it it not a variable

I've searched through a lot of forums and can't seem to find an answer. Any ideas?

foreach (CheckBoxes i in main)
            {
                CheckBox x = new CheckBox();
                x.Text = i.Data;
                x.Checked = i.Condition;
                x.Location = new Point(main_start_location_x, main_start_location_y);
                x.Size.Width = 570;
                tabControl1.TabPages["main_checklist_tab"].Controls.Add(x);
                main_start_location_y += 40;
            }
2
  • 1
    Try setting the size using a Size object, i.e. x.Size = new Size(...) Feb 18, 2013 at 22:33
  • instead of calculate and set the Location for each of the checkboxes, put them in a layout panel like FlowLayoutPanel or TableLayoutPanel. Feb 18, 2013 at 22:51

2 Answers 2

2

You must set the size of a control by using the control's Width and Height properties or the Size property, but not the Width and Height of the Size property, since that is passed by value and will have no effect.

1) Control.Size = new Size(width, height);

or

2) Control.Width = width;

1
  • Thank you - I'm not sure why I didn't think of that before :)
    – Wheeler
    Feb 18, 2013 at 22:47
1

instead of:

x.Size.Width = 570;

Use this:

x.Width = 570;

if you want to set the whole size at once, do this:

x.Size = new Size(570, 20);

or this:

x.Width = 570;
x.Height = 20;
1
  • Awesome - thank you for the quick response! I'm not sure why I didn't think of that before :)
    – Wheeler
    Feb 18, 2013 at 22:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.