Results 1 to 14 of 14

Thread: [RESOLVED] How to create a Bouncing Picture Box Control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Resolved [RESOLVED] How to create a Bouncing Picture Box Control

    I am a newbie in .NET, can any one help me in creating a bouncing Picture Box Control.

    I want the picture box control bounces top-bottom

    I tried like this

    Code:
    picturebox1.top=top+3
    picturebox.top=top-6
    but its not working properly...can any one help
    Last edited by riteshtechie; Jan 10th, 2010 at 06:48 AM.
    < advertising link removed by moderator >

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: How to create a Bouncing Picture Box Control

    Oh it worked, it just happened so fast that you couldn't see it. If you want to do animation, you'll need to use the System.Windows.Forms.Timer.

    You can find it in the components section of your toolbox in the designer window. Just drag it onto your form. In the properties window the default tick is about 100ms, so you'll want to increase that to maybe 200. Double click on the timer to create the elapsed event. In that event you can put this code, for example, and your box will move up the form until it disappears:

    Code:
    picturebox1.top -= 2
    The timer is by default enabled, I think. You should probably change that to Disabled, and then enable the timer manually with a button.

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: How to create a Bouncing Picture Box Control

    Quote Originally Posted by riteshtechie View Post
    I am a newbie in .NET, can any one help me in creating a bouncing Picture Box Control.

    I want the picture box control bounces top-bottom

    I tried like this

    Code:
    picturebox1.top=top+3
    picturebox.top=top-6
    but its not working properly...can any one help
    As MM says you'll need a timer, but also neither of those lines is likely to work.

    I'm assuming your picturebox is called PictureBox1? In which case the first line should be :

    Code:
    PictureBox1.Top = PictureBox1.Top + 3
    NB - just saying that something "isn't working properly" doesn't really give people much to go on. You should really say whether the code compiles and runs but just doesn't do what you expect, or if it doesn't compile at all, or if it compiles and runs but then throws an error at runtime. I suspect in this case it won't compile at all.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to create a Bouncing Picture Box Control

    First up, that code is taking the current value of the form's Top property and adding to it or subtracting from it. If you want to change the PictureBox's Top property then you have to start with the current value of the PictureBox's Top property, not the form's. E.g.
    vb.net Code:
    1. PictureBox1.Top = PictureBox1.Top + 3
    or, more simply:
    vb.net Code:
    1. PictureBox1.Top += 3
    Now that you know HOW to move the PictureBox, you have to decide WHEN to move it. If you want animation like that in Windows Forms, usually that means adding a Timer to your form and then making a change, e.g. move a PictureBox, in the Tick event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: How to create a Bouncing Picture Box Control

    Thank you all but I am still having Problem, I want that my Picturebox control would only bounce for the period the mouse is hovered over it and idea which event I should use
    < advertising link removed by moderator >

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: How to create a Bouncing Picture Box Control

    Well in your mouse enter event you would enable the timer and in your mouse leave event you would disable it.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: How to create a Bouncing Picture Box Control

    I tried following which make my program Unstable

    vb Code:
    1. Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
    2.         Timer1.Enabled = True
    3.     End Sub
    4.  
    5.  
    6.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    7. lab:
    8.         PictureBox1.Top += 2
    9.         PictureBox1.Top -= 2
    10.         GoTo lab
    11.  
    12.     End Sub
    13.  
    14.  
    15.     Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
    16.         Timer1.Enabled = False
    17.     End Sub
    < advertising link removed by moderator >

  8. #8
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: How to create a Bouncing Picture Box Control

    I tried following which make my program Unstable
    Again... please explain what you mean by unstable. People are happy to help but it makes it easier if we don't have to guess what your problem is.

    In your timer_tick event you are making the picture box go down and then up and you are using goto (horror!) to loop. You should only make the box go down or up once per tick. If you need to alternate direction you could just have a static variable indicating direction and reverse it after each tick.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: How to create a Bouncing Picture Box Control

    Sorry for that....

    When I run the program it goes into infinite loop, ie my Picturecontrol goes on bouncing even when I have moved my cursor from that control.

    And after sometime I get Not responding Error
    < advertising link removed by moderator >

  10. #10
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: How to create a Bouncing Picture Box Control

    Yes thats because you have an infinite loop in your timer tick event. Try something like

    Code:
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Static Direction As String = "down"
    
            If Direction = "down" Then
                PictureBox1.Top += 2
                Direction = "up"
            Else
                PictureBox1.Top -= 2
                Direction = "down"
            End If
    
        End Sub

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: How to create a Bouncing Picture Box Control

    You made my day keystone_paul
    Thanks
    < advertising link removed by moderator >

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: How to create a Bouncing Picture Box Control

    Its really appreciating that you helped me out and i am again sorry for asking again....

    I have 10 PictureBox actually so how to that for every Picture Box (I think I have to keeptrack on which control starts the timer)
    < advertising link removed by moderator >

  13. #13
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: How to create a Bouncing Picture Box Control

    If you want to keep track of which one of a number of picture boxes activated the timer then when you start the timer you can set the timer's tag property to reference the picture box that triggered it,

    Code:
    Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
    
           Timer1.Enabled = True
           Timer1.Tag = sender
    
        End Sub
    then use this in the timer event :

    Code:
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Static Direction As String = "down"
            Dim ActiveControl As PictureBox
    
            ActiveControl = DirectCast(Timer1.Tag, PictureBox)
    
            If Direction = "down" Then
                ActiveControl.Top += 2
                Direction = "up"
            Else
                ActiveControl.Top -= 2
                Direction = "down"
            End If
    
        End Sub
    If your question is resolved please remember to use the Thread Tools menu towards the top of the page to mark it resolved.

    Cheers
    Last edited by keystone_paul; Jan 10th, 2010 at 06:54 AM.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: [RESOLVED] How to create a Bouncing Picture Box Control

    The app for which, I needed this code is created thanks for your help

    Take a look (If MOD feel its spamming delete the link)
    http://beingpc.com/2010/01/god-mode-creator-launched/
    < advertising link removed by moderator >

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width