Change the location of an object programmatically

前端 未结 6 880
南笙
南笙 2020-12-01 12:19

I\'ve tried the following code:

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

to change the location of a panel that I mad

6条回答
  •  心在旅途
    2020-12-01 12:47

    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.

提交回复
热议问题