简体   繁体   中英

C# - Drag and Dropping PictureBox onto a TableLayoutPanel

I'm building a scrabble-like game in C# where I have a collection of "letters" (PictureBox controls), and need to drag/drop them onto the playing board (TableLayoutPanel displaying a grid of PictureBox controls).

I initially tried to use the DoDragDrop() method on the letter PictureBox inside the MouseDown event handler, but couldn't get the control to do anything once I started dragging. I referenced this code project .

After exhausting that, I'm trying to "manually" move the PictureBox using MouseDown, MouseMove, and MouseUp event handlers:

private void letter1PictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (sender is PictureBox && e.Button == MouseButtons.Left)
        {
            isDragging = true;
            dragOffsetX = e.X;
            dragOffsetY = e.Y;
        }
    }

private void letter1PictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            PictureBox letter = (PictureBox)sender;

            letter.BringToFront();
            letter.Top += (e.Y - dragOffsetY);
            letter.Left += (e.X - dragOffsetX);
        }
    }

My problem is that, when trying to drag the PictureBox onto my TableLayoutPanel (or anywhere outside of the GroupBox it's contained in), the PictureBox disappears behind the control/panel.

http://i.stack.imgur.com/uAJV9.png

I would've thought the BringToFront() call would've prevented this, no?

Help is much appreciated.

Does it help to send the TableLayoutPanel to back in the design window? I guess you probably tried that already..

Who is the parent of the tile (PictureBox)? Is it the TableLayoutPanel, or is it the Form?

You might need to make TableLayoutPanel the parent of the PictureBox. I don't think this type of Panel "likes" objects in front of it.

Also, isn't it easier/better to snap the tile into position yourself?

int posX=(posX/tileSize)*tileSize;
int posY=(posY/tileSize)*tileSize;

That integer division/multiplication should snap it into a grid, but only apply it once the tile has been released by the user. To know what cell the tile is in, don't multiply by tileSize at the end:

int cellX=(posX/tileSize);
int cellY=(posY/tileSize);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM