Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have four picture boxes
1 2
4 3

placed in the format above.
when i click the button it rotates my picture boxes only once like this
2 3
1 4

and timer will start and count up to 3 seconds then auto clicks my button to rotate again but in this case my code is wrong please help so i can rotate this picture boxes random number of times?

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoveBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (pictureBox1.Location.Equals(pictureBox1.Location))
            {
                //This swaps positions of picture boxes clockwise
                // first -90 degree rotation of four boxes
                pictureBox1.Location = new Point(0, 160);
                pictureBox2.Location = new Point(0, 0);
                pictureBox3.Location = new Point(180, 0);
                pictureBox4.Location = new Point(180, 160);
                moveTimer1.Enabled = true;
            }
            else if (pictureBox1.Location.Equals(pictureBox3.Location))
            {
                //This swaps positions of picture boxes clockwise
                // second -90 degree rotation of four boxes
                pictureBox1.Location = new Point(0, 0);
                pictureBox2.Location = new Point(180, 0);
                pictureBox3.Location = new Point(180, 160);
                pictureBox4.Location = new Point(0, 160);
            }
        }

        private void moveTimer1_Tick(object sender, EventArgs e)
        {
            int timer = Convert.ToInt32(label1.Text);
            label1.Text = Convert.ToString(timer + 1);
            while (timer == 3)
            {
                  moveTimer1.Enabled = false;
                  button1.PerformClick();
            }
            
        }
    }
}
Posted
Updated 16-May-17 11:46am
v2
Comments
Richard MacCutchan 16-May-17 16:16pm    
while (timer == 3)
You will only move the boxes if timer is equal to 3, which will only happen once.

1 solution

Assuming that you want to rotate/move images every 3 seconds, you have to provide a method which get two random pictureboxes and replaces images in them instead of replacing pricuteboxes locations (unless, you want to move pictureboxes).

On button click:
C#
Timer1.Interval = 3000 // Here 3000 milliseconds = 3 seconds  
Timer1.Start()  


On Timer tick you have to call (for example): MovePicture() method.
Think of it! It seems to be easy to implement. You have to define a List<Point> to be able to shuffle them by using Random class[^]:

C#
//create initial list of picture locations
List<System.Drawing.Point> picLocations = new List<System.Drawing.Point>()
	{
		new System.Drawing.Point(0, 0),
		new System.Drawing.Point(180, 0),
		new System.Drawing.Point(180, 160),
		new System.Drawing.Point(0, 160)
	};


//...

//MovePicture method
//randomize 
Random r = new Random();
int f = r.Next(0,2); //get random number between 0 and 1 - a picture location from first row
System.Drawing.Point first = picLocations[f]; 
int s = r.Next(2,4); //get random number between 2 and 3 - a picture location from second row
System.Drawing.Point second = picLocations[s]; 

//replace locations
picLocations[f] = second;
picLocations[s] = first;

//finall
PictureBox1.Location = picLocation[0];
PictureBox2.Location = picLocation[1];
PictureBox3.Location = picLocation[2];
PictureBox4.Location = picLocation[3];


Sample picture locations one timer execution:
X    Y
180 160 
180 0 
0   0 
0   160 


Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900