0

(really new to coding so any answers please show coding for) I have a program where the user presses images of letters to form there answer. I want these pictures of the letters to change position after each one is pressed. I have coded it so it does it once but don't know how to code it so it keeps changing after more letters are pressed. I tried to add coding that would do that but it hasn't worked. *sorry I know its a bit of coding

  public Level1()
        {
            InitializeComponent();
        }

        
        Point one;
        Point two;
        Point three;
        Point four;
        Point five;
        Point six;
        Point seven;
        Point eight;
        Point nine;
        Point ten;
        Point eleven;
        Point twelve;
        Point thirteen;
        Point fourteen;
        Point fifteen;
        Point sixteen;
        Point seventeen;
        Point eighteen;
private void SwitchButtonLocation()
        {
            picW.Location = two;
            picU.Location = three;
            picI.Location = four;
            picF.Location = five;
            picB.Location = six;
            picH.Location = seven;
            picA.Location = eight;
            picP.Location = nine;
            picE.Location = ten;
            picY.Location = eleven;
            picM.Location = twelve;
            picZ.Location = thirteen;
            picX.Location = fourteen;
            picO.Location = fifteen;
            picS.Location = sixteen;
            picK.Location = seventeen;
            picC.Location = eighteen;
            picJ.Location = one;

            picW.Location = twelve;
            picU.Location = four;
            picI.Location = ten;
            picF.Location = sixteen;
            picB.Location = one;
            picH.Location = eighteen;
            picA.Location = two;
            picP.Location = five;
            picE.Location = fifteen;
            picY.Location = three;
            picM.Location = six;
            picZ.Location = seventeen;
            picX.Location = seven;
            picO.Location = eight;
            picS.Location = nine;
            picK.Location = eleven;
            picC.Location = thirteen;
            picJ.Location = fourteen;
private void Level1_Load(object sender, EventArgs e)
        {
            

            one = new Point(picW.Location.X, picW.Location.Y);
            two = new Point(picU.Location.X, picU.Location.Y);
            three = new Point(picI.Location.X, picI.Location.Y);
            four = new Point(picF.Location.X, picF.Location.Y);
            five = new Point(picB.Location.X, picB.Location.Y);
            six = new Point(picH.Location.X, picH.Location.Y);
            seven = new Point(picA.Location.X, picA.Location.Y);
            eight = new Point(picP.Location.X, picP.Location.Y);
            nine = new Point(picE.Location.X, picE.Location.Y);
            ten = new Point(picY.Location.X, picY.Location.Y);
            eleven = new Point(picM.Location.X, picM.Location.Y);
            twelve = new Point(picZ.Location.X, picZ.Location.Y);
            thirteen = new Point(picX.Location.X, picX.Location.Y);
            fourteen = new Point(picO.Location.X, picO.Location.Y);
            fifteen = new Point(picS.Location.X, picS.Location.Y);
            sixteen = new Point(picK.Location.X, picK.Location.Y);
            seventeen = new Point(picC.Location.X, picC.Location.Y);
            eighteen = new Point(picJ.Location.X, picJ.Location.Y);

            one = new Point(picA.Location.X, picA.Location.Y);
            two = new Point(picY.Location.X, picY.Location.Y);
            three = new Point(picU.Location.X, picU.Location.Y);
            four = new Point(picP.Location.X, picP.Location.Y);
            five = new Point(picM.Location.X, picM.Location.Y);
            six = new Point(picX.Location.X, picX.Location.Y);
            seven = new Point(picO.Location.X, picO.Location.Y);
            eight = new Point(picS.Location.X, picS.Location.Y);
            nine = new Point(picI.Location.X, picI.Location.Y);
            ten = new Point(picK.Location.X, picK.Location.Y);
            eleven = new Point(picW.Location.X, picW.Location.Y);
            twelve = new Point(picC.Location.X, picC.Location.Y);
            thirteen = new Point(picJ.Location.X, picJ.Location.Y);
            fourteen = new Point(picE.Location.X, picE.Location.Y);
            fifteen = new Point(picF.Location.X, picF.Location.Y);
            sixteen = new Point(picZ.Location.X, picZ.Location.Y);
            seventeen = new Point(picH.Location.X, picH.Location.Y);
            eighteen = new Point(picB.Location.X, picB.Location.Y);
}
lex
  • 13
  • 2
  • Try using OOP: create a class to hold single letter information (position, text, etc.) and class to manage collection of such (methods to move, maybe add/delete, etc.). The key would be method `Shuffle()` which will randomize positions, you have to call it after any letter is clicked. – Sinatr Aug 18 '20 at 07:26
  • would this mean that sometimes my images could overlap – lex Aug 18 '20 at 07:29
  • Do you need them to overlap? I guess you don't, that means `Shuffle()` should not result in overlapping letters. – Sinatr Aug 18 '20 at 07:40
  • There are already topics around of how to shuffle, e.g. [shuffle list](https://stackoverflow.com/q/273313/1997232). If you use index as letter positions, then on screen position can be calculated from the index with simple formula: `int x = (index % n) * width`, `int y = index / n * height`, where `n` is number of items in single line, `height` and `width` - size of single item on screen. – Sinatr Aug 18 '20 at 07:41

1 Answers1

0

According to your description, when you click the image, you want to move its position. In addition, you want these pictures not to overlap.

You could refer to the following code:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace PicMove
{
public partial class Form1 : Form
{
    List<PictureBox> list = new List<PictureBox>();
    Random r = new Random();
    public Form1()
    {
        InitializeComponent();           
    }

    private void Form1_Load(object sender, EventArgs e)
    {
       
        foreach (Control picturebox in this.Controls)
        {
            PictureBox pic = picturebox as PictureBox;
            if (pic != null)
                picturebox.Click += new EventHandler(PictureBox_Click); 
        }
                       
    }
    private void PictureBox_Click(object sender, EventArgs e)
    {           
        foreach (Control picturebox in this.Controls)
        {               
            PictureBox pic = picturebox as PictureBox;
            if (pic != null) 
            {
               //record all the picturebox information and store them in a collection.
                list.Add(pic);
                Controls.Remove(pic);                   
            }                    
        }
        regenerate(list);
    }

    public void regenerate(List<PictureBox> list)
    {
        foreach (var p in list) 
        {
            p.Location = new Point(r.Next(0, this.ClientSize.Width - p.Width + 1), r.Next(0, this.ClientSize.Height - p.Height + 1));
           // Judge whether the currently generated picturebox overlaps with other generated pictureboxes
            foreach (Control picturebox in this.Controls)
            {
                if (p.Bounds.IntersectsWith(picturebox.Bounds))
                {
                      Controls.Remove(p);
                       // Overlap, regenerate this picturebox
                       regenerate(p);                                   
                }
                else
                {
                    Controls.Add(p);
                }
            }
        }
                        
    }
    public void regenerate(PictureBox p) 
    {
        p.Location = new Point(r.Next(0, this.ClientSize.Width - p.Width + 1), r.Next(0, this.ClientSize.Height - p.Height + 1));
        foreach (Control picturebox in this.Controls)
        {
            if (p.Bounds.IntersectsWith(picturebox.Bounds))
            {
                Controls.Remove(p);
                regenerate(p);            
            }
            else
            {
                Controls.Add(p);
            }
        }
    }
   
}
}

The running result

dear_vv
  • 2,350
  • 1
  • 4
  • 13
  • this is what I was asking but how do u move all of them at once – lex Aug 19 '20 at 09:41
  • @lex When clicking any picturebox, you can try to remove all pictureboxes from the form, then regenerate them. I have updated my code, you can check if it works for you. – dear_vv Aug 20 '20 at 02:15
  • If you encounter other problems, please feel free to contact me. – dear_vv Aug 21 '20 at 06:47