471,326 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

adding control programmatically

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

Dec 20 '05 #1
4 2300
John,

The reason that this doesn't work is because Location is of type Point,
which is a value type. When you return value types from properties or
methods (through the return value, not ref parameters), a copy is made and
returned to you.

That being said, when you do this:

rb.Location.X= 67;

You are setting the X property/field on a copy returned to you. To
perform this operation, you would need to do this:

// Store the location.
Point temp = rb.Location;

// Set the X value.
temp.X = 67;

// Set the location back.
rb.Location = temp;

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<jo************@googlemail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
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

Dec 20 '05 #2
Jon
The Location property is of type System.Drawing.Point (a struct). This
means that accessing the button's Location property returns a copy of
the struct, and since this is not assigned to a variable it cannot be
modified. (Even if the compiler let this through you would still have
been modifying a temporary variable, and it would have no effect on the
button).

Instead, use

rb.Location = new Point(x, y);

Jon.

Dec 20 '05 #3
Hi,
Additionaly to Nicholas comment you can use Left to set a new position.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<jo************@googlemail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
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

Dec 20 '05 #4
[reply to all]

thanks all for your help. I went for the option Nicholas posted. It's
got past the compiler and it's now up to me to make sure my algorithm
is right.

Thanks again all

John

Dec 28 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mike Malter | last post: by
3 posts views Thread by Jim Heavey | last post: by
18 posts views Thread by Praveen Ramesh | last post: by
2 posts views Thread by johnb41 | last post: by
2 posts views Thread by Wayne Deleer | last post: by
5 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.