How to place a control relative to another control


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I'm having a heck of a time figuring this out.



I have a control that is within a tab, that is in toostripcontainer. I want to programmatically place a control (a label) such that the upper left corner of the label is right below the controls lower left corner. Now this is trivial if they are both in the same container, in this case the tab page. Alas, that is not the case. The label is in the form's control collection, not the tabs.



My first try was to get the point that is at the lower left of the control in the tab, translate it to screen coordinates, then translate it back to client coordinates, but relative to the form:



Dim lowerLeftClient As Location = New Location(theControl.Left, theControl.Top + theControl.Height)

Dim lowerLeftScreen As Location = theControl.PointToScreen(lowerLeftClient)

Dim lowerLeftForm As Location = theControl.ParentForm.PointToClient(lowerLeftScreen)

theLabel.Location = lowerLeftForm




This typically results in the label being too low and too far right.



Then I thought maybe the PointToClient relative to the form was including the borders and header, but when setting the location, these were not. So I attempted to take this into consideration by comparing the Size of the form to the ClientSize. I figured the X difference \ 2 should be the left border width (or close) and maybe the Y difference minus the border width would be the header height. Alas, this degraded into finding magic numbers...which changed on every form/layout. Crazy



So, if you've followed this far, I'm hoping you can help me figure out how to retrieve a location relative to the form itself, of a control buried within other control(s). Ermm



Thanks!







Replies
Peter Denton
Peter Denton
StrataFrame User (147 reputation)StrataFrame User (147 reputation)StrataFrame User (147 reputation)StrataFrame User (147 reputation)StrataFrame User (147 reputation)StrataFrame User (147 reputation)StrataFrame User (147 reputation)StrataFrame User (147 reputation)StrataFrame User (147 reputation)
Group: Forum Members
Posts: 77, Visits: 787
G'day

I believe that the location of a control is relative to its parent control/container, so it would seem that you have to accumulate the locations of the control you want to label with that of it's parent, and its parent's parent until you get back to the form.

Then you have to subtract the accumulation of the locations of the parent of control/container that will contain the label, and its parent etc.

Then after allowing for the size of the label (or should that be the size of the control being labeled, is the location top left or bottom left?) and the gap, this should give you the offset you want.

This should be a completely general solution.

Please forgive me if I'm telling you something you had already figured out.

Peter

P.S. A wise man once said "For every complex problem there is a simple and elegent solution that's wrong". I hope I'm not proposing that solution.

StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Yep, Peter is right... Your best bet would be to recursively add the .Location of each parent up the chain until you get to the form.  If you are off a few pixels, then you might need to take into consideration the .BorderStyle property of the parent controls.  For instance, setting a Panel's BorderStyle to FixedSingle shifts everything by 1 pixel, while setting it to Fixed3D shifts it by 2 pixels, so you would need to offset your values by the totals of the pixels (yeah, pain in the posterior).
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search