Change the location of an object programmatically

Ahoura Ghotbi picture Ahoura Ghotbi · Dec 3, 2011 · Viewed 126.3k times · Source

I've tried the following code:

 this.balancePanel.Location.X = this.optionsPanel.Location.X;

to change the location of a panel that I made in design mode while the program is running but it returns an error:

Cannot modify the return value of 'System.Windows.Forms.Control.Location' because it is not a variable.

So how can I do it?

Answer

Mark Byers picture Mark Byers · Dec 3, 2011

The Location property has type Point which is a struct.

Instead of trying to modify the existing Point, try assigning a new Point object:

 this.balancePanel.Location = new Point(
     this.optionsPanel.Location.X,
     this.balancePanel.Location.Y
 );