1
votes

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.

2
Well, you're doing it wrong. That starts by using a control to display an image. A window. Lots of hassle moving a window around, you are fighting the Z-order. Don't do it that way. A Scrabble tile is not a control. It is a bitmap. You display a bitmap with e.Graphics.DrawImage() in the Paint event. Moving it only requires drawing it somewhere else.Hans Passant

2 Answers

0
votes

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

0
votes

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);