[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.
Re: Snap form to outer edge of another form.
Does anyone have any ideas for this? :)
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. :)
Re: Snap form to outer edge of another form.
Quote:
Originally Posted by
JugglingReferee
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:
Private Sub MediaLibrary_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
If (Me.Location.X - Form1.Location.X) < 10 Then
'Snap to edge of form somehow... Maybe:
Me.Location = New Point(Form1.Location.X - (Me.Width + 5), Me.Location.Y)
End If
End Sub
Re: Snap form to outer edge of another form.
Quote:
Originally Posted by
thegreatone
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:
Private Sub MediaLibrary_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
If (Me.Location.X - Form1.Location.X) < 10 Then
'Snap to edge of form somehow... Maybe:
Me.Location = New Point(Form1.Location.X - (Me.Width + 5), Me.Location.Y)
End If
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.
Re: Snap form to outer edge of another form.
Quote:
Originally Posted by
JugglingReferee
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.
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:
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:
Private Sub Form2_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
If (Me.Location.X - (Form1.Location.X + Form1.Width)) < 15 And ((Form1.Location.X + Form1.Width) - 15) < Me.Location.X Then
'Snap to edge of form somehow... Maybe:
Me.Location = New Point(Form1.Location.X + (Form1.Width + 5), Me.Location.Y)
End If
If (Me.Location.Y - Form1.Location.Y) < 15 And ((Form1.Location.Y) - 15) < Me.Location.Y Then
Me.Location = New Point(Me.Location.X, Form1.Location.Y + 5)
End If
End Sub
Re: [RESOLVED] Snap form to outer edge of another form.
Glad to help you get it working. :D :eek: