adding controls programmatically

  • Thread starter Thread starter johnmmcparland
  • Start date Start date
J

johnmmcparland

I am trying to add controls (e.g. radio buttons, group boxes) to a
windows form in a method which I call from the constructor.

However, when I do things like;

RadioButton rb= new RadioButton();
....
rb.Location.X= 67;

this.Controls.Add(rb); // this is the Windows Form instance

I am told that;

"Cannot modify the return value of
'System.Window.Forms.Control.Locatoin' because it is not a variable"

I have tried to use CreateControl() but it doesn't seem to have any
effect.

If anyone knows how to do this please help asap

John
 
Adding controls programmatically is never a good idea. First, you should be
using the CreateControl method and the form has to be in design view. One of
the problems you will probably run into is there is a lifetime limit of 754
controls on a form; so, each time you run the form and create a control, that
deducts from the limit. Eventually, the form will fail.

I don't know exactly what you are trying to do, but here are some
alternatives to consider. Create enough radio buttons on the form to handle
any eventuality and make them all Visible = No. Then make them visible as
you need them rather than create them. You can even change the caption of
their labels if necessary. Another way would be to use a combo box or list
box to allow the user to make a selection.
 
Back
Top