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
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
Woot, glad you got it Smile
Matt Searles
Matt Searles
StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)StrataFrame Beginner (1 reputation)
Group: Forum Members
Posts: 1, Visits: 1
I was having a similar problem, in that I wanted to place a borderless form just under a label when it was clicked, and couldn't get the true screen co-ordinates of the control. Here's a recursive method as suggested by Peter to do that. It doesn't take into account borderwidths, but it gives the location of the control on the screen.

Point AddPoints(Point p1, Point p2)

{

return new Point(p1.X + p2.X, p1.Y + p2.Y);

}

Point SubtractPoints(Point p, Point from)

{

return new Point(from.X - p.X, from.Y - p.Y);

}

private void ScreenLocation(Control ctl, ref Point screenLocation)

{

screenLocation = AddPoints(ctl.Location, screenLocation);

if (ctl.Parent != null)

{

ScreenLocation(ctl.Parent, ref screenLocation);

}

else

{

screenLocation = PointToScreen(SubtractPoints(ctl.Location, screenLocation));

}

}


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
I had to do this just the other day, and I ended up doing it like this:

To find the bottom left corner:

Point pt = this.myLabel.PointToScreen( new Point( 0, this.myLabel.Height ) );
BorderlessForm f = new BorderlessForm( );
f.StartPosition = FormStartPosition.Manual;
f.Location = pt;
f.Show( );

Maybe that will help.  It gets the screen coordinates with respect to the label (since the method was called on the label), so you need the left edge with respect to the label (0), and you need the bottom edge with respect to the label (Height).

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