Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the location of an object programmatically

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?

like image 884
Ahoura Ghotbi Avatar asked Dec 03 '11 18:12

Ahoura Ghotbi


2 Answers

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  ); 
like image 82
Mark Byers Avatar answered Sep 28 '22 05:09

Mark Byers


Location is a struct. If there aren't any convenience members, you'll need to reassign the entire Location:

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

Most structs are also immutable, but in the rare (and confusing) case that it is mutable, you can also copy-out, edit, copy-in;

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

Although I don't recommend the above, since structs should ideally be immutable.

like image 41
Marc Gravell Avatar answered Sep 28 '22 04:09

Marc Gravell