How Make A PictureBox Bounce Everywhere In C#

How Make A PictureBox Bounce Everywhere In C#
Okay I think you have seen this thing in VB.NET or Visual Basic 5.0/6.0 :) so i was at my class and one of my class guy asked me how he could make a picturebox bounce everywhere in your screen so i was bulbed what i mean from bulb-ed is i got an idea , so i gave him the codes and i wanted to give you all the codes too so lets start you can use a picturebox in this program


Add a image to the picturebox and change the form property Form_BorderStyle = NONE and double and you need a timer so drag and drop one and set it's interval to 0 , now click your form and add these codes ,
        
        //Author : Mohamed Shimran
        //Blog : http://ultimateprogrammingtutorials.blogspot.com
        public Boolean top_ = true;
        public Boolean left_ = true;
        public int Speed =25;
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (top_ == true) Form1.ActiveForm.Top += Speed; else Form1.ActiveForm.Top -= Speed;
                if (left_ == true) Form1.ActiveForm.Left += Speed; else Form1.ActiveForm.Left -= Speed;

                if (Form1.ActiveForm.Top >= Screen.PrimaryScreen.Bounds.Height - 40) top_ = false;
                if (Form1.ActiveForm.Left >= Screen.PrimaryScreen.Bounds.Width - 37) left_ = false;
                if (Form1.ActiveForm.Top < -5) top_ = true;
                if (Form1.ActiveForm.Left < -5) left_ = true;
            }
            catch
            {
                timer1.Enabled = false;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Hide();
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

now debug and see it in action

3 comments

Can you post Vb code

Reply

I converted the code to vb.net :

Public top_ As Boolean = True
Public left_ As Boolean = True
Public Speed As Integer = 25
Private Sub timer1_Tick(sender As Object, e As EventArgs)
Try
If top_ = True Then
Form1.ActiveForm.Top += Speed
Else
Form1.ActiveForm.Top -= Speed
End If
If left_ = True Then
Form1.ActiveForm.Left += Speed
Else
Form1.ActiveForm.Left -= Speed
End If

If Form1.ActiveForm.Top >= Screen.PrimaryScreen.Bounds.Height - 40 Then
top_ = False
End If
If Form1.ActiveForm.Left >= Screen.PrimaryScreen.Bounds.Width - 37 Then
left_ = False
End If
If Form1.ActiveForm.Top < -5 Then
top_ = True
End If
If Form1.ActiveForm.Left < -5 Then
left_ = True
End If
Catch
timer1.Enabled = False
End Try
End Sub

Private Sub pictureBox1_Click(sender As Object, e As EventArgs)
timer1.Enabled = True
End Sub

Private Sub Form1_Deactivate(sender As Object, e As EventArgs)
timer1.Enabled = False
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs)
Hide()
End Sub

Private Sub Form1_Activated(sender As Object, e As EventArgs)
timer1.Enabled = True
End Sub

Reply

This barcode scanner api with one method supports JPEG, GIF and PNG images and can read many types of barcodes like QRCode, PDF417, and Data Matrix.

Reply

Post a Comment