I have a datagrid with image cells (and hidden text cells) in which I want to be able to drag and drop images from one cell to another, this part I have working, what I can not get to work is the cursor during the drag and drop. While dragging I would like the image to replace the cursor similar to what occurs when you drag a file on your windows desktop. Currently what is occurring is that the cursor is flickering between the custom image I would like to use and the default drag cursor (pointer with a small rectangle). I'm a bit lost on how to fix this. Note that I'm no expert in coding and much of my code is put together from googling what I am trying to complete and piecing things together.
GIF of issue: https://gfycat.com/AgileImpressiveHermitcrab Note that my recorder didn't capture everything, it flickers much faster than shown and flickers while the mouse is not moving as well, non stop (only when holding down the mouse button)
private DataGridViewCell drag_Image; //This is the initial image dragged
private object HoldingImage; //Stored target image
private DataGridViewCell drag_ID; //ID tied with initial image
private object HoldingID; //ID tied with stored target image
private Bitmap DragCursor; //This is the initial dragged image, set as the cursor
private string DragIndicator; //Attempting a new trigger for drag event
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{//This code is not needed for functionality, what is the point here?
//if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// If the mouse moves outside the rectangle, start the drag.
//if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
{
// Proceed with the drag and drop, passing in the list item.
//DragDropEffects dropEffect = dataGridView1.DoDragDrop(dataGridView1.Rows[dataGridView1.CurrentRow.Index],DragDropEffects.Move);
}
}
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
//This is where the cursor begins to flicker in the debugger, it cycles through this code
Bitmap DragCursor = new Bitmap(@"C: \Users\******\Desktop\Items\Organizer\Planner\Planner\1.png");
DragCursor.MakeTransparent(Color.White);
Cursor cur = new Cursor(DragCursor.GetHicon());
Cursor.Current = cur;
e.Effect = DragDropEffects.Move;
DragIndicator = "1";
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
// The mouse locations are relative to the screen, so they must be
// converted to client coordinates.
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
// Get the row index of the item the mouse is below.
DataGridView.HitTestInfo hti = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
DataGridViewCell targetCell = dataGridView1[hti.ColumnIndex, hti.RowIndex];
DataGridViewCell targetCellID = dataGridView1[hti.ColumnIndex + 1, hti.RowIndex];
// If the drag operation was a move then remove and insert the row.
//if (e.Effect == DragDropEffects.Move)
if (DragIndicator == "1") //The new trigger works though we still can not removed all DragDropEffect.Move lines, as this disables dragging
{
if (SwapBtn.Enabled == false)
{
//Swap mode
HoldingImage = targetCell.Value;
targetCell.Value = drag_Image.Value;
drag_Image.Value = HoldingImage;
HoldingID = targetCellID.Value;
targetCellID.Value = drag_ID.Value;
drag_ID.Value = HoldingID;
dataGridView1.Refresh();
DragIndicator = "0";
}
else
{
//TODO Insert mode
}
}
}
public void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X, e.Y);
drag_Image = dataGridView1[hti.ColumnIndex, hti.RowIndex];
drag_ID = dataGridView1[hti.ColumnIndex + 1, hti.RowIndex];
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(drag_Image, DragDropEffects.Move);
}
}