Results 1 to 8 of 8

Thread: [RESOLVED] Snap form to outer edge of another form.

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4700 x4
    Posts
    1,333

    Resolved [RESOLVED] Snap form to outer edge of another form.

    As the title suggests i want to snap one forms edge to another when within 10pixels of each-other.

    I did try a little with LocationChanged event, but i couldn't get this to respind in the way i wanted.

    Can anyone throw up an example, or something?

    Ideally i want to snap a second form to the rightmost edge of my first form.

    Thanks.
    Zeegnahtuer?

  2. #2

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4700 x4
    Posts
    1,333

    Re: Snap form to outer edge of another form.

    Does anyone have any ideas for this?
    Zeegnahtuer?

  3. #3
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: Snap form to outer edge of another form.

    Can you post the code you did use - I'll glad take a look as I am interested in this functionality as well.
    PC #1: Athlon64 X2+Vista64+VS2008EE+.NET3.5 #2: XP (all flavours Ghost'd)+VS2008EE+.NET3.5
    Help Files: HelpMaker · Dr. Explain · Create Icons: IcoFX


    "Whoever eats my flesh and drinks my blood has eternal life, and I will raise him on the last day." John 6:54

  4. #4

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4700 x4
    Posts
    1,333

    Re: Snap form to outer edge of another form.

    Quote Originally Posted by JugglingReferee View Post
    Can you post the code you did use - I'll glad take a look as I am interested in this functionality as well.
    I've since removed it from my Form.

    Here's something i came up with afterwards to check the locations, but i just can't seem to get my head around it at all:
    VB.Net Code:
    1. Private Sub MediaLibrary_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
    2.         If (Me.Location.X - Form1.Location.X) < 10 Then
    3.             'Snap to edge of form somehow... Maybe:
    4.             Me.Location = New Point(Form1.Location.X - (Me.Width + 5), Me.Location.Y)
    5.         End If
    6.     End Sub
    Zeegnahtuer?

  5. #5
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: Snap form to outer edge of another form.

    Quote Originally Posted by thegreatone View Post
    I've since removed it from my Form.

    Here's something i came up with afterwards to check the locations, but i just can't seem to get my head around it at all:
    VB.Net Code:
    1. Private Sub MediaLibrary_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
    2.         If (Me.Location.X - Form1.Location.X) < 10 Then
    3.             'Snap to edge of form somehow... Maybe:
    4.             Me.Location = New Point(Form1.Location.X - (Me.Width + 5), Me.Location.Y)
    5.         End If
    6.     End Sub
    The If statement is only executed if the left edge of Me is within 10 pixels of Form1.

    And then, you set the forms such that Me is to the left of Form1?

    Why didn't this code work? It look right to me, although I'm not certain where the "5" comes from.
    PC #1: Athlon64 X2+Vista64+VS2008EE+.NET3.5 #2: XP (all flavours Ghost'd)+VS2008EE+.NET3.5
    Help Files: HelpMaker · Dr. Explain · Create Icons: IcoFX


    "Whoever eats my flesh and drinks my blood has eternal life, and I will raise him on the last day." John 6:54

  6. #6

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4700 x4
    Posts
    1,333

    Re: Snap form to outer edge of another form.

    Quote Originally Posted by JugglingReferee View Post
    The If statement is only executed if the left edge of Me is within 10 pixels of Form1.

    And then, you set the forms such that Me is to the left of Form1?

    Why didn't this code work? It look right to me, although I'm not certain where the "5" comes from.
    The code does work, but i'm at a loss mentally how i can use it to only be within 10 of each X value of Form1.

    Basically, within 10 of Form1.Location.X on either side.

    Also, the 5 Pixels part is taking into account the forms Border.
    Zeegnahtuer?

  7. #7

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4700 x4
    Posts
    1,333

    Re: Snap form to outer edge of another form.

    Oh man, i was going about the addition/subtraction all wrong, rearranged it a little for the second test, and bam, it works great.

    Replace the line in the IF statement above with :

    vb.net Code:
    1. If (Me.Location.X - (Form1.Location.X + Form1.Width)) < 10 And ((Form1.Location.X + Form1.Width) - 10) < Me.Location.X Then
    This checks for < 10 either side, allowing docking.

    Full working code (Docks at 15pixels)
    VB.Net Code:
    1. Private Sub Form2_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
    2.         If (Me.Location.X - (Form1.Location.X + Form1.Width)) < 15 And ((Form1.Location.X + Form1.Width) - 15) < Me.Location.X Then
    3.             'Snap to edge of form somehow... Maybe:
    4.             Me.Location = New Point(Form1.Location.X + (Form1.Width + 5), Me.Location.Y)
    5.         End If
    6.         If (Me.Location.Y - Form1.Location.Y) < 15 And ((Form1.Location.Y) - 15) < Me.Location.Y Then
    7.             Me.Location = New Point(Me.Location.X, Form1.Location.Y + 5)
    8.         End If
    9.     End Sub
    Last edited by thegreatone; May 31st, 2009 at 05:26 AM.
    Zeegnahtuer?

  8. #8
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: [RESOLVED] Snap form to outer edge of another form.

    Glad to help you get it working.
    PC #1: Athlon64 X2+Vista64+VS2008EE+.NET3.5 #2: XP (all flavours Ghost'd)+VS2008EE+.NET3.5
    Help Files: HelpMaker · Dr. Explain · Create Icons: IcoFX


    "Whoever eats my flesh and drinks my blood has eternal life, and I will raise him on the last day." John 6:54

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