How to Shake a Form in VB.NET

This is a tutorial that will teach you how to create a form shaker in vb.net. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add two Buttons named Button1 for Left-Right shake and Button2 for Up-Down shake button. You must design your interface like this: output 3. Now, we will do the coding. First, we will create a module named Shaker. Declare and instantiate the following variables.
  1. Private frm As Form
  2. Private howMuch As Integer
  3. Private th As Threading.Thread
  4. Private timeForPause As Integer
  5.  
  6. Public Shared MOVE_UP_DOWN As Integer = 0
  7. Public Shared MOVE_LEFT_RIGHT As Integer = 1
  8.  
  9. Private direction As Integer
We will create a property named timeForPauseBetweenMove to pause between move of the shake.
  1. Public Property timeForPauseBetweenMove As Integer
  2. Get
  3. Return timeForPause
  4. End Get
  5. Set(ByVal value As Integer)
  6. timeForPause = value
  7. End Set
  8. End Property
To get the instantiation of our Form1, we will make a sub procedure named New.
  1. Sub New(ByVal frm As Form, ByVal howMuchToMove As Integer)
  2. Me.frm = frm
  3. howMuch = howMuchToMove
  4. End Sub
We will create a property named moveDirection to determine the direction of the shake.
  1. Public Property timeForPauseBetweenMove As Integer
  2. Get
  3. Return timeForPause
  4. End Get
  5. Set(ByVal value As Integer)
  6. timeForPause = value
  7. End Set
  8. End Property
Now, here's the code for shaking the form.
  1. Sub shake()
  2.  
  3. Dim tempLoc As Point = frm.Location
  4. Dim startLoc As Point = New Point(frm.Location.X, frm.Location.Y)
  5.  
  6.  
  7. Select Case moveDirection
  8.  
  9. Case MOVE_LEFT_RIGHT
  10.  
  11. For a As Integer = howMuch To 0 Step -1
  12.  
  13. Dim poss As New ff(AddressOf formPosition)
  14. frm.Invoke(poss, New Point(startLoc.X - a, startLoc.Y))
  15. Threading.Thread.Sleep(timeForPauseBetweenMove)
  16.  
  17. frm.Invoke(poss, New Point(startLoc.X + a, startLoc.Y))
  18. Threading.Thread.Sleep(timeForPauseBetweenMove)
  19.  
  20. Next
  21.  
  22. Case MOVE_UP_DOWN
  23.  
  24. For a As Integer = howMuch To 0 Step -1
  25.  
  26. Dim poss As New ff(AddressOf formPosition)
  27. frm.Invoke(poss, New Point(startLoc.X, startLoc.Y - a))
  28. Threading.Thread.Sleep(timeForPauseBetweenMove)
  29.  
  30. frm.Invoke(poss, New Point(startLoc.X, startLoc.Y + a))
  31. Threading.Thread.Sleep(timeForPauseBetweenMove)
  32.  
  33. Next
  34.  
  35. End Select
  36.  
  37. Dim pos As New ff(AddressOf formPosition)
  38. frm.Invoke(pos, startLoc)
  39.  
  40. End Sub
To locate the position of the form.
  1. Sub formPosition(ByVal p As Point)
  2. frm.Location = p
  3. End Sub
To start shaking the form. Here's the code below:
  1. Sub startShake()
  2.  
  3. Try
  4. th = New System.Threading.Thread(AddressOf shake)
  5. th.Start()
  6.  
  7. Catch ex As Exception
  8. MessageBox.Show(ex.Message)
  9. End Try
  10. End Sub
4. Then, will go back to code for our Form. Call the Shaker module and initialize it.
  1. Dim s As New Shaker(Me, 50)
In your Button1_Click, have this code below to trigger to shake your form left and right with 50ms to shake.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. s.moveDirection = Shaker.MOVE_LEFT_RIGHT
  3. s.timeForPauseBetweenMove = 50
  4. s.startShake()
  5. End Sub
In your Button2_Click, have this code below to trigger to shake your form up and down with 50ms to shake.
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. s.moveDirection = Shaker.MOVE_UP_DOWN
  3. s.timeForPauseBetweenMove = 50
  4. s.startShake()
  5. End Sub
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment