What is the difference between control's bounds and setting X,Y,Width,Height of a control?

StackOverflow https://stackoverflow.com/questions/15231958

  •  18-03-2022
  •  | 
  •  

Question

So, lets say I have a panel on a winform and I want it displayed at particular point and should have a specific width and height on the winform. Plus, I want to do it during run-time.

So, what is the difference and the right way to move and set panel's dimensions?

This way:

Panel1.bounds.X:=10;
Panel1.bounds.Y:=10;
Panel1.bounds.width:=100;
Panel1.bounds.height:=103;

Or This way:

Panel1.Left := 10;
Panel1.Top := 10;
Panel1.width:=100;
Panel1.height:=103;

Or Both ways should have the same effect on the panel1.

I am trying to figure out what really is wrong with my program...Although I have asked a question specific to my problem, no one even attempted to answer or even able to leave comment. So, I am asking bits and piece of question to understand my problem.

Était-ce utile?

La solution

If you want to set the Bounds, you need to do it with a rectangle. (Please forgive any syntax mistakes in my examples; my Delphi is a little rusty.)

BoundsRect: Rectangle;
BoundsRect.X = 10;
BoundsRect.Y = 10;
BoundsRect.Width := 100;
BoundsRect.Height := 103;
Panel1.Bounds := BoundsRect;

Typically, you'd use that if you want to set or change multiple properties. If you just want to set one or two properties, you can use Width, Top, etc.

One other difference is that every time you set one of those properties (either Bounds, or one of the individual properties), it causes a lot of work behind the scenes (moving and redrawing the window, etc.). Setting the Bounds property from the rectangle will be less work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top