Wheeler
Wheeler

Reputation: 462

Cannot resize checkbox when dynamically adding checkboxes to form

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

Upvotes: 0

Views: 2253

Answers (2)

John Willemse
John Willemse

Reputation: 6698

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;

Upvotes: 2

Adriano Carneiro
Adriano Carneiro

Reputation: 58615

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;

Upvotes: 1

Related Questions