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?
Printable View
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?
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:In the first case, you use Point.Empty because the location of the Panel relative to itself is (0, 0).vb.net Code:
Dim p1 = myPanel.PointToScreen(Point.Empty) Dim p2 = myForm.PointToScreen(myPanel.Location)
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:
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.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
BB