|
-
Jan 10th, 2010, 04:00 AM
#1
Thread Starter
Lively Member
[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 >
-
Jan 10th, 2010, 05:24 AM
#2
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.
-
Jan 10th, 2010, 05:27 AM
#3
Re: How to create a Bouncing Picture Box Control
 Originally Posted by riteshtechie
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.
-
Jan 10th, 2010, 05:28 AM
#4
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:
PictureBox1.Top = PictureBox1.Top + 3
or, more simply: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.
-
Jan 10th, 2010, 05:59 AM
#5
Thread Starter
Lively Member
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 >
-
Jan 10th, 2010, 06:00 AM
#6
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.
-
Jan 10th, 2010, 06:16 AM
#7
Thread Starter
Lively Member
Re: How to create a Bouncing Picture Box Control
I tried following which make my program Unstable
vb Code:
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter Timer1.Enabled = True End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick lab: PictureBox1.Top += 2 PictureBox1.Top -= 2 GoTo lab End Sub Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave Timer1.Enabled = False End Sub
< advertising link removed by moderator >
-
Jan 10th, 2010, 06:28 AM
#8
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.
-
Jan 10th, 2010, 06:30 AM
#9
Thread Starter
Lively Member
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 >
-
Jan 10th, 2010, 06:35 AM
#10
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
-
Jan 10th, 2010, 06:45 AM
#11
Thread Starter
Lively Member
Re: How to create a Bouncing Picture Box Control
You made my day keystone_paul
Thanks
< advertising link removed by moderator >
-
Jan 10th, 2010, 06:49 AM
#12
Thread Starter
Lively Member
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 >
-
Jan 10th, 2010, 06:50 AM
#13
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.
-
Jan 10th, 2010, 03:19 PM
#14
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|