473,394 Members | 1,739 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,394 software developers and data experts.

Silly question?

Hi there,

This question might be silly to you, but anyway...

How can I move a control at runtime? Object.Location.X = 48; doesn't seem to
work. Do
I really need the code "Object.Location = new
System.Drawing.Point(48,...)???

thnx
Oct 31 '06 #1
3 2705

C# Beginner wrote:
Hi there,

This question might be silly to you, but anyway...

How can I move a control at runtime? Object.Location.X = 48; doesn't seem to
work. Do
I really need the code "Object.Location = new
System.Drawing.Point(48,...)???

thnx
If Point is immutable, then yup. Sucks, don'it? For extra pain, try
resizing a Font. If point is mutable (and Google tells me that it is),
you can alternately do

myPoint = Object.Location;
myPoint.X = 48;
Object.Location = myPoint; //actually copies

Of course, that's the general solution that will serve you in all
similar cases. In this particular case, MS has given us handy
helper-properties for doing that stuff: control.left (X) and
control.top (Y).

So just say control.Left = 48;

really, it's as clear as mud.

Oct 31 '06 #2

C# Beginner wrote:
Hi there,

This question might be silly to you, but anyway...

How can I move a control at runtime? Object.Location.X = 48; doesn't seem to
work. Do
I really need the code "Object.Location = new
System.Drawing.Point(48,...)???
Funny, we were just talking about this in the "Struct vs Class"
discussion. :-)

The answer is that yes, you have to say:

myControl.Location = new Point(48, myControl.Location.Y);

The reasons are subtle, but perfectly logical.

Remember that Point is a value type. That is, it acts like an integer
or a double. Whenever you assign a Point, you assign a copy. Whenever
you pass it to a method as an argument, you pass a copy. Most
importantly in this case, whenever you return it from a method as a
result, you return a copy. Points are _not_ reference objects like most
things in C# / .NET: you can't fiddle with a Point and expect that
other points change their values as a result.

So, look back at the original code:

myControl.Location.X = 48;

Seems harmless enough, but think about what's really going on here.
Location is a property. So, under the covers, what's really happening
is that you're calling a function called Location_get() to get the
Point value that represents the location of the control. So, you call
the function to get the location Point value, which returns *a copy of*
the value, on the stack. You then modify that copy to set it's X
coordinate to 48. The value then... is thrown away, because it was
never copied into a variable anywhere. In effect, you're doing this:

Point p = myControl.Location;
p.X = 48;

which, of course, doesn't modify anything about myControl.

So, since Location is a property, and any attempt to get its value
results in a copy of the value, not the original value, the only way to
give it a new value is to assign it wholesale:

myControl.Location = new Point(...);

Oct 31 '06 #3
It seems I got confused, spending too much time looking at the properties
window instead of examining the control members (a real blooper).

thnx anyway
"C# Beginner" <de********@orange.nlschreef in bericht
news:45***********************@news.wanadoo.nl...
Hi there,

This question might be silly to you, but anyway...

How can I move a control at runtime? Object.Location.X = 48; doesn't seem
to work. Do
I really need the code "Object.Location = new
System.Drawing.Point(48,...)???

thnx

Nov 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pjotr Wedersteers | last post by:
This may be a silly question, but I was wondering. If 10 clients fill out a form on my page simultaneaously and hit submit, how does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
4
by: Jenny | last post by:
Hi you al I have two very silly question The first one is In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba I cannot seem to find...
4
by: musosdev | last post by:
I think I'm having a dim day and should just go back to bed, but alas, I cant do that.... I'm writing a peice of code to create XHTML compliant documents using System.IO, there's probably an...
6
by: Adam Honek | last post by:
Hi, I've looked over and over and in MSDN 2005 and just can't find what is a really simple answer. How do I select the first value in a combo box to be shown? I tried the .selectedindex -1...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
2
by: Willem Voncken | last post by:
Hi guys, might be a silly question, but i'm wondering whether it is even possible to generate access violations when using C#? I'm new at c# programming, but i have some experience with c++....
1
by: Pontifex | last post by:
Hi all, Has anyone devised a simple method for passing a string to an external script? For instance, suppose I want to produce a title bar that is very fancy and I don't want my main HTML...
2
by: toton | last post by:
Hi, This is a silly question related to syntax only. I have a template class with template member function, How to write it separately in a file (not translation unit, just want to separate...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
5
by: Ben Bacarisse | last post by:
Newbie <newbie@gmail.comwrites: <snip> The version you posted in comp.programming has the correct loop. Why change it? -- Ben.
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.