Home > Software design >  draw picture box and get coordinates
draw picture box and get coordinates

Time:01-28

i'm newbie here and also in c#.

my project is to create a box in grid view. then when click desired box, i'll get the box coordinate or position and box will change the colour. when click another box, the previous box colour will change to original.

the box will resize when total size for rows x cols more than panel2 size.

        for (int cols = 0; cols < COLUMNS; cols  )
        {
            for (int rows = 0; rows < ROWS; rows  )
            {                    
                PictureBox newPic = new PictureBox();
                newPic.Height = HEIGHT;
                newPic.Width = WIDTH;
                newPic.BackColor = Color.Maroon;

                int x = cols * (HEIGHT   SPACE);
                int y = rows * (WIDTH   SPACE);
                newPic.Location = new Point(x   SPACE, y   SPACE);

                newPic.Click  = NewPic_Click;

                items.Add(newPic);
                this.panel2.Controls.Add(newPic);

            }
        }

enter image description here

CodePudding user response:

Just for color switching, you only need the PictureBox which has been clicked on. It is stored in the sender parameter.

I you want the coordinates, you need to store some information on the PictureBox. You don't want to specify 50 handlers. The way I would do is; to make use of the Tag property of a Control.

Your for-loop would be something like:

for (int cols = 0; cols < COLUMNS; cols  )
{
    for (int rows = 0; rows < ROWS; rows  )
    {                    
        PictureBox newPic = new PictureBox();
        newPic.Height = HEIGHT;
        newPic.Width = WIDTH;
        newPic.BackColor = Color.Maroon;

        // instead of the coordinates, store the indices (for col, row)
        newPic.Tag = new Point(cols, rows);

        // I would use the Width on the cols, instead of the Height.
        int x = cols * (WIDTH   SPACE); 
        int y = rows * (HEIGHT   SPACE);
        newPic.Location = new Point(x   SPACE, y   SPACE);
        newPic.Click  = NewPic_Click;

        items.Add(newPic);
        this.panel2.Controls.Add(newPic);

    }
}

And your handler would be something like:

// a field to store the previous selected picturebox.
private PictureBox _currentPictureBox = null;

private void NewPic_Click(object sender, EventArgs e)
{
    // the picturebox which has been clicked, is stored in the sender object, but you need to cast it to a PictureBox.
    PictureBox pb = (PictureBox)sender;

    // just for the extra use of the Tag.
    var location = (Point)pb.Tag;
    // location.X Contains the Column index
    // location.Y Contains the Row index



    // did we have a previous picturebox?
    if(_currentPictureBox != null)
    {
        // change the previous pictureBox back to Maroon
        _currentPictureBox.BackColor = Color.Maroon;
    }

    // change the current to blue
    pb.BackColor = Color.Blue;

    // store the new one as the current. (so we can revert it)
    _currentPictureBox = pb;
}

I haven't tested it, only in a notepad. So there might be some typo's. But I hope you get the idea.

  • Related