• We just launched and are currently in beta. Join us as we build and grow the community.

How to Shake a Form in VB.NET

idenshz

Tokenization Advocate
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
151
Likes
55
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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:

shakedes.png


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. Public

    Shared

    MOVE_UP_DOWN As

    Integer

    =

    0
  6. Public

    Shared

    MOVE_LEFT_RIGHT As

    Integer

    =

    1

  7. 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. Dim

    tempLoc As

    Point =

    frm.

    Location
  3. Dim

    startLoc As

    Point =

    New

    Point(

    frm.

    Location

    .

    X

    , frm.

    Location

    .

    Y

    )


  4. Select

    Case

    moveDirection

  5. Case

    MOVE_LEFT_RIGHT

  6. For

    a As

    Integer

    =

    howMuch To

    0

    Step

    -

    1

  7. Dim

    poss As

    New

    ff(

    AddressOf

    formPosition)
  8. frm.

    Invoke

    (

    poss, New

    Point(

    startLoc.

    X

    -

    a, startLoc.

    Y

    )

    )
  9. Threading.

    Thread

    .

    Sleep

    (

    timeForPauseBetweenMove)

  10. frm.

    Invoke

    (

    poss, New

    Point(

    startLoc.

    X

    +

    a, startLoc.

    Y

    )

    )
  11. Threading.

    Thread

    .

    Sleep

    (

    timeForPauseBetweenMove)

  12. Next

  13. Case

    MOVE_UP_DOWN

  14. For

    a As

    Integer

    =

    howMuch To

    0

    Step

    -

    1

  15. Dim

    poss As

    New

    ff(

    AddressOf

    formPosition)
  16. frm.

    Invoke

    (

    poss, New

    Point(

    startLoc.

    X

    , startLoc.

    Y

    -

    a)

    )
  17. Threading.

    Thread

    .

    Sleep

    (

    timeForPauseBetweenMove)

  18. frm.

    Invoke

    (

    poss, New

    Point(

    startLoc.

    X

    , startLoc.

    Y

    +

    a)

    )
  19. Threading.

    Thread

    .

    Sleep

    (

    timeForPauseBetweenMove)

  20. Next

  21. End

    Select

  22. Dim

    pos As

    New

    ff(

    AddressOf

    formPosition)
  23. frm.

    Invoke

    (

    pos, startLoc)

  24. 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. Try
  3. th =

    New

    System.

    Threading

    .

    Thread

    (

    AddressOf

    shake)
  4. th.

    Start

    (

    )

  5. Catch

    ex As

    Exception
  6. MessageBox.

    Show

    (

    ex.

    Message

    )
  7. End

    Try
  8. 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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

324,125

324,133

Top