AdorableVB
AdorableVB

Reputation: 1393

Simple Location animation of winforms

been searching around.. but haven't seen something I want..
I have seen fade in animation - through opacity property
seen extending forms, bouncing too - through .size property.

But no documentations on moving forms..
say, I want my form to appear from Point(0,0) to the center

'something like this
Do While Me.Location isNot Centered
   From Point(0,0) + 1
Loop

Is there a way to do that?
I want an animation that slides.. out of nowhere.. like jQuery lol

Upvotes: 0

Views: 73

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

I am not sure if this is what you are wanting to achieve, you will notice if you are incrementing by 1 that the speed of movement is not extremely fast. I am subtracting the width of the form from the edge of your screen, and then incrementing it by one till it is equal or greater than half of the width of the screen minus the half of the width of the form which should have it centered for you. This example is just a simple form with a Timer added.

Public Class Form1

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.StartPosition = FormStartPosition.Manual
        Me.Location = New Point(0 - Me.Width, Me.Location.Y)
        Timer1.Interval = 1

    End Sub

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Me.Location.X >= (Screen.PrimaryScreen.Bounds.Width / 2 - Me.Width / 2) Then
            Timer1.Stop()
        Else
            Me.Location = New Point(Me.Location.X + 1, Me.Location.Y)
        End If
    End Sub
End Class

or you could combine it with the opacity that you mentioned above to create some interesting effects.

Public Class Form1

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.StartPosition = FormStartPosition.Manual
        Me.Location = New Point(0 - Me.Width, Me.Location.Y)
        Me.Opacity = 0
        Timer1.Interval = 1

    End Sub

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Me.Location.X >= (Screen.PrimaryScreen.Bounds.Width / 2 - Me.Width / 2) Then
            Timer1.Stop()
        Else
            Me.Location = New Point(Me.Location.X + 4, Me.Location.Y)
            Me.Opacity = Me.Location.X / (Screen.PrimaryScreen.Bounds.Width / 2 - Me.Width / 2)
        End If
    End Sub
End Class

Upvotes: 1

Related Questions