OMGSOMETAL
OMGSOMETAL

Reputation: 126

Animated sliding form VB.Net

EDIT:

So now my form location is established as per ZackRyan's answer but I still can't get it to move back to original location after it slides left.

This is the code I expect to move the form to original location

    Private Sub Label7_Click(sender As Object, e As EventArgs) Handles Label7.Click

    Me.Close()


    'Testing.StartPosition.CenterScreen
    For MoveLeft = Form2.Location.X To 30 Step 1
        Form2.Left = MoveLeft
        Form2.Refresh()
        Threading.Thread.Sleep(0)
    Next


End Sub

This is the original location:

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim xLoc = Me.Location.X
    Dim yLoc = Me.Location.Y
    Me.Location = New Point(xLoc, yLoc)

End Sub

------------------------------------------//--------------------------------------------------

I'm creating a windows app where a form moves to the left smoothly, all ok with this. My problem is bringing it back to it's initial position.

The form is loaded with default position set to CenterScreen I then use the following to move it to the left:

Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
  For MoveLeft = FormStartPosition.CenterScreen To 18 Step 1
    Me.Left -= MoveLeft
    Me.Refresh()
    Threading.Thread.Sleep(1)
  Next

To make it go back I'm trying the inverse:

Private Sub Label9_Click(sender As Object, e As EventArgs) Handles Label9.Click
  For MoveLeft = FormStartPosition.CenterScreen To 18 Step 1
    Me.Left = MoveLeft
    Me.Refresh()
    Threading.Thread.Sleep(1)
  Next

This only works partially. The form moves to the right which is what is required, but instead of moving from the current position to CenterScreen, if moves completely at the bottom of the page.

I basically need it to return to its original center screen position smoothly.

Upvotes: 1

Views: 2519

Answers (1)

Aousaf Rashid
Aousaf Rashid

Reputation: 5738

Simply store the form's position(on form load) in some variables...

E.g.

  Dim xLoc = MyForm.Location.X
  Dim yLoc = MyForm.Location.Y

'Or

 Dim xLoc = MyForm.Bounds.Left
 Dim yLoc = MyForm.Bounds.Top

'or

Dim loc As Point = MyForm.Location

then use it as follows :

 MyForm.Location = New Point(xLoc,yLoc)

'or

 MyForm.Location = New Point(loc)

And one last thing , i really don't find your code fascinating enough to slide in a form .. You can easily use a Timer for that :

  Dim Withevents tmr As New Forms.Timer
  Dim myXLocation as integr
  Private sub Btn_Click()
    Tmr.start()

  Private sub tmr_Tick()
    myXloc= myXloc+ 1
    MyForm.Location=New Point(myXloc,yloc)
    If myXloc>= 190 Then  'change 190 to whatever u want :)
     Tmr.Stop()   
    End if 

Upvotes: 1

Related Questions