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.
I would've thought the BringToFront() call would've prevented this, no?
Help is much appreciated.