-
Jan 27th, 2016, 05:43 PM
#1
Thread Starter
Lively Member
Position and size form to match panel on another form
Hello, I have two forms. Form1 has a panel docked along its left side. Form2 is borderless, topmost and I want to position it right over the panel's location as well as matching it's size. Is this possible to do?
-
Jan 27th, 2016, 05:53 PM
#2
Thread Starter
Lively Member
Re: Position and size form to match panel on another form
Nevermind, I immediately figured it out after posting haha. I simply got the screen coordinates of the panel and went from there, rather than just using the panels form location.
-
Jan 27th, 2016, 06:22 PM
#3
Re: Position and size form to match panel on another form
Originally Posted by theryan722
Nevermind, I immediately figured it out after posting haha. I simply got the screen coordinates of the panel and went from there, rather than just using the panels form location.
That is the correct option. As you haven't specified how you did that, I'll make a recommendation in case you did it some other way. You should use the PointToScreen method of some control. That control would logically be either the Panel itself or its direct parent, which is probably the form. Those would look like this:
vb.net Code:
Dim p1 = myPanel.PointToScreen(Point.Empty) Dim p2 = myForm.PointToScreen(myPanel.Location)
In the first case, you use Point.Empty because the location of the Panel relative to itself is (0, 0).
-
Jan 27th, 2016, 06:32 PM
#4
Re: Position and size form to match panel on another form
Yes. Basically, you need to get the Panel's bounds in screen coordinates (with RectangleToScreen) and set Form2's Bounds property to equal that. The Panel's screen bounds could change when its parent form moves, or when the panel itself changes size of position within the form. Fortunately, you can code for all these in a single Event Handler sub on Form1:
Code:
Private Sub Form1_StuffChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged, Panel1.LocationChanged, Panel1.SizeChanged
Dim rect As Rectangle = Me.RectangleToScreen(Panel1.Bounds)
With Form2
.Bounds = rect
'set other Form2 properties here or in the Designer
.Hide()
.Show(Me)
End With
End Sub
In most situations you shouldn't set the TopMost property, because it won't prevent other windows from getting between Form1 and Form2. Instead, make Form1 the Owner of Form2 by the "Me" argument in the Show sub. That ensures the two forms will stay locked together in the Z direction. Alternatively you can set the Owner property directly (e.g. Form2.Owner = Me). Depending on what you are doing, you may also want to set Form2's ShowInTaskBar property to False.
BB
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|