Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code

C#
private void timer1_Tick(object sender, EventArgs e)
     {
pictureBox1.Location = new Point(pictureBox1.Location.X += 1, pictureBox1.Location.Y);
     }


and this is my error

VB
Error   1   Cannot modify the return value of 'System.Windows.Forms.Control.Location' because it is not a variable  Z:\Longitudinal Studio\Longitudinal Studio\Form1.cs 73  47  Longitudinal Studio
Posted

"Because the Point class is a value type (Structure in Visual Basic, struct in Visual C#), it is returned by value, meaning accessing the property returns a copy of the upper-left point of the control. So, adjusting the X or Y properties of the Point returned from this property will not affect the Left, Right, Top, or Bottom property values of the control. To adjust these properties set each property value individually, or set the Location property with a new Point."

Source :
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.location.aspx[^]

So this is trouble statement
pictureBox1.Location.X += 1
 
Share this answer
 
Comments
Suresh Suthar 12-Apr-12 2:41am    
You beat me to it. Nice answer. 5!
Lakamraju Raghuram 12-Apr-12 2:43am    
Team work
Thank you Suresh.
VJ Reddy 12-Apr-12 3:02am    
Good answer. +5
The solution 1 given by Lakamraju Raghuram is very good answer and straight to the point.
I want to add that, even if pictureBox1.Location.X can be modified, it is redundant in the following statement as the same value is again passed as an argument to the parameter of the constructor, which will be set in the constructor again.
C#
pictureBox1.Location = new Point(pictureBox1.Location.X += 1, pictureBox1.Location.Y);

So,
pictureBox1.Location.X + 1 will be sufficient instead of
pictureBox1.Location.X += 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900