473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2359
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Malter | last post by:
I take it that the only way to add rows to a DataList control is through DataBind? If not, how do I manually add rows to a DataList control? I have searched everywhere and can't find an example...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
18
by: Praveen Ramesh | last post by:
Hi, Is there any way to add the @Assembly reference to the aspx files programmatically from inside a custom control (when it gets dropped on to the page from the toolbox)? I have a custom...
2
by: johnb41 | last post by:
I want to create a textbox programmatically and add the SAME textbox to to different panels. This code does not work: Dim tb As New TextBox tb.text = "xyz" Panel1.Controls.Add(tb)...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
2
by: Wayne Deleer | last post by:
Hi All, I'm currently working on a project, and I've come to a part where I want to add data to a GridView. BUT, I want to do it programmatically, with alot of control over everything. In my...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
4
by: alun65 | last post by:
I'm attempting to programmatically build up some HTML in the code behind. Like so: // Create Hyperlink HyperLink link = new HyperLink(); link.NavigateUrl = "nice cat"; link.Text = "Cats...
6
by: alun65 | last post by:
I'm having trouble programmatically adding a user control and then setting some of it's server controls. I add the user control to the code behind and add it to a placeholder: protected void...
5
by: Nathan Sokalski | last post by:
I have css that would normally be placed in style tags in the header of the Master page that I want to add programmatically for a specific Web Content Form (the *.aspx page). How do I do this for a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.