SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

How would I make my form bounce around the screen?

Last post 10-28-2017 4:50 PM by sbridewell. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 10-09-2017 5:29 PM

    How would I make my form bounce around the screen?

    I'm making a little prank program in VB and I want to make the form bounce around the edges of the screen.

    But none of the tutorials are helping me!

    Can you tell me how to do this?

     

    I'm also using #Develop 4.4, as I believe that is the latest version with a form designer.

  • 10-28-2017 4:50 PM In reply to

    Re: How would I make my form bounce around the screen?

    Version 5 does include a form designer, but only for C#. 4.4 is indeed the most recent version which supports VB.net in the form designer.

    I'm more of a C# person so my VB.net might not be the most elegant possible, but this should do what you want. First you need an enumeration like this

    Public Enum Direction
        UpLeft
        UpRight
        DownLeft
        DownRight
    End Enum

    Then put a Timer on your form and give it an event handler like

        Sub Timer1Tick(sender As Object, e As EventArgs)
            
            ' Shift the window
            If direction = Direction.UpLeft Then
                Location = New Point(Location.X - xshift, Location.Y - yshift)
            Else If direction = Direction.UpRight Then
                Location = New Point(Location.X + xshift, Location.Y - yshift)
            Else If direction = Direction.DownLeft Then
                Location = New Point(Location.X - xshift, Location.Y + yshift)
            Else If direction = Direction.DownRight Then
                Location = New Point(Location.X + xshift, Location.Y + yshift)
            End If
            
            ' Check if we've hit the edge of the screen and need to change direction
            
            If Location.X < 0 Then
                ' we've hit the left edge so we must be going UpLeft or DownLeft
                If direction = Direction.UpLeft Then
                    direction = Direction.UpRight
                Else
                    direction = Direction.DownRight
                End If
            End If
            
            If Location.Y < 0 Then
                ' we've hit the top edge so we must be going UpLeft or UpRight
                If direction = Direction.UpLeft Then
                    direction = Direction.DownLeft
                Else
                    direction = Direction.DownRight
                End If
            End If
            
            If Location.X + Width > Screen.PrimaryScreen.Bounds.Right Then
                ' we've hit the right edge so we must be going UpRight or DownRight
                If direction = Direction.UpRight Then
                    direction = Direction.UpLeft
                Else
                    direction = Direction.DownLeft
                End If
            End If
            
            If Location.Y + Height > Screen.PrimaryScreen.Bounds.Bottom Then
                ' we've hist the bottom edge so we must be going DownLeft or DownRight
                If direction = Direction.DownLeft Then
                    direction = Direction.UpLeft
                Else
                    direction = Direction.UpRight
                End If
            End If
            
        End Sub

    And your form will also need some private members to support this

        Private direction As Direction
        Private Const xShift As Int32 = 1
        Private const yShift As Int32 = 1

    Finally, don't forget to start the timer. You may want to play with the timer's interval and the xShift and yShift values. I found an interval of 10 and xShift and yShift values of 1 to produce quite good results.

    Simon

Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.