Results 1 to 4 of 4

Thread: Position and size form to match panel on another form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    70

    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?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    70

    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.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Position and size form to match panel on another form

    Quote Originally Posted by theryan722 View Post
    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:
    1. Dim p1 = myPanel.PointToScreen(Point.Empty)
    2.  
    3. 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).

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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
  •  



Click Here to Expand Forum to Full Width