Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I reposition a control in relative coordinates?

Status
Not open for further replies.

gib99

Programmer
Mar 23, 2012
51
0
0
CA
Hello,

I have a container that has three panels in it: HeaderPanel, ControlPanel, ButtonPanel. They are all vertically aligned in that order.

I'm having trouble repositioning the ButtonPanel. Any time I add a control to the ControlPanel, I have to shift the ButtonPanel down to make room for the control so that it's visible.

I try repositioning the ButtonPanel like this:

ButtonPanel.Location = new Point(ButtonPanel.Location.X, ControlPanel.Location.Y + ControlPanel.Height);

But this does not position the ButtonPanel where I expect it to be.

I tried experimenting with things like this:

ButtonPanel.Location = new Point(ButtonPanel.Location.X, 1);

in which case the ButtonPanel didn't show up at all.

Then it occurred to me that this repositioning might be setting the location in absolute coordinates (which means that setting Y to 1 would put the ButtonPanel way above its container, and therefore not visible). I tested this with incremental values for Y until the bottom edge of the ButtonPanel finally started showing up within its container.

So I'm wondering why is it that when I get the Y value of ControlPanel, it gives it to me in relative coordinates (i.e. relative to its container) but if I try to set the location of the ButtonPanel (or any panel I presume), it sets it to absolute coordinates?

How can I change this so that the coordinates I give it when I'm repositioning the control are relative to the control's container?
 
As far as I know it should always be relative to the container control.

I assume HeaderPanel, ControlPanel, and ButtonPanel all share a common container (the Form perhaps, or maybe another panel)?

Here is what most likely happened when you slowly increased Y until you saw the bottom edge: It was relative to the container control (1 being the TOP) - but you could not see it because it ButtonPanel was behind the HeaderPanel and the ControlPanel.

ButtonPanel.Location = new Point(ButtonPanel.Location.X, ControlPanel.Location.Y + ControlPanel.Height);

But this does not position the ButtonPanel where I expect it to be.

When you say that - do you mean that it is slightly higher than what you expected it to be? This is easily caused by not taking into account the Margin of both the ButtonPanel and the ControlPanel, plus any additional spacing you may have had during the initial layout of those panels. So you might see something like this:

Code:
ButtonPanel.Location = new Point(ButtonPanel.Location.X, ControlPanel.Location.Y + ControlPanel.Height + ControlPanel.Margin.Bottom + ButtonPanel.Margin.Top + anyExistingSpace);

Another simpler way to keep them all together and automatically adjust is to stick HeaderPanel, ControlPanel, and ButtonPanel all within a FlowLayoutPanel - with the FlowDirection set to TopDown. Then when the height of ControlPanel changes, ButtonPanel should automatically be pushed down.
 
Thanks for that Borvik,

Sorry for not replying earlier but I got busy and forgot that I posted this.

In any case, the problem is solved (not by me though so I'm not sure how it was solved).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top