Search code examples
c#visual-studiopictureboxwindows-forms-designer

How to store locations properly


I have this form that allows the user to create pictureboxes by clicking and dragging them around with mouse movement. How can I store the coordinates (locations) of where the pictureboxes are each time one is added or when one is moved to a new spot? I am trying to store them in a list as strings for exportation.

private void Form_MouseClick(object sender, MouseEventArgs e)
{
    // create new control
    PictureBox pictureBox = new PictureBox();
    pictureBox.Location = new Point(e.X, e.Y);
    pictureBox.BackColor = Color.Red;
    this.Controls.Add(pictureBox);
    // bind event for each PictureBox
    pictureBox.MouseDown += pictureBox_MouseDown;
    pictureBox.MouseUp += pictureBox_MouseUp;
    pictureBox.MouseMove += pictureBox_MouseMove;
}


Point mouseDownPoint = Point.Empty;
Rectangle rect = Rectangle.Empty;
bool isDrag = false;

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        mouseDownPoint = e.Location;
        rect = (sender as PictureBox).Bounds;
    }
}

private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        if (isDrag)
        {
            isDrag = false;
            (sender as PictureBox).Location = rect.Location;
            this.Refresh();
        }
        reset();
    }
}

private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        isDrag = true;
        rect.Location = this.PointToClient((sender as PictureBox).PointToScreen(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y)));
        this.Refresh();
    }
}

private void reset()
{
    mouseDownPoint = Point.Empty;
    rect = Rectangle.Empty;
    isDrag = false;
}

Solution

  • To save the loaction of each picturebox, you can define a dictionary to save the key-value pair.

    First, define the dictionary box_location_Pairs as a global variable.

    Then add the new item to dictionary when "click to a create new picturebox".

    Dictionary<PictureBox, Point> box_location_Pairs = new Dictionary<PictureBox, Point>();
    
    // ...
    
    private void Form_MouseClick(object sender, MouseEventArgs e)
    {
        PictureBox pictureBox = new PictureBox();
        pictureBox.Location = new Point(e.X, e.Y);
        box_location_Pairs.Add(pictureBox, pictureBox.Location);
        // ...
    }
    

    If the picturebox is moved, modify its value.

    private void pictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (isDrag)
            {
                isDrag = false;
                (sender as PictureBox).Location = rect.Location;
                box_location_Pairs[sender as PictureBox] = rect.Location; // modifty key-value pair
                this.Refresh();
            }
            reset();
        }
    }