-1

I need to run code when I dismiss(end) dragging click. How this event is called?

Here is main example, please find the below screenshot for more information:

before drag

I made that I can drag the car on other picture boxes like below this:

after drag

Repeat again - I need to know what EVENT is then you dismiss to drag on picture box?

C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67

2 Answers2

2

There is no event for when the drag is released on a control, but you don't really need one. This is what I did to simulate what (I think) you're looking for. I used code courtesy of this stackoverflow answer

private Point? _mouseLocation;

private void Form1_Load(object sender, EventArgs e)
{
    this.pictureBox1.MouseDown += this.pictureBox1_MouseDown;
    this.pictureBox1.MouseUp += this.pictureBox1_MouseUp;
    this.pictureBox1.MouseMove += this.pictureBox1_MouseMove;    
}

void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{  
    if ( this._mouseLocation.HasValue)
    {
        this.pictureBox1.Left = e.X + this.pictureBox1.Left - this._mouseLocation.Value.X;
        this.pictureBox1.Top = e.Y + this.pictureBox1.Top - this._mouseLocation.Value.Y;
    }
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    this._mouseLocation = null;
}

void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    //Check if you've left-clicked if you want
    this._mouseLocation = e.Location;

}

Setting the mouse location to null with this._mouseLocation = null; is your "drag released" code.

Community
  • 1
  • 1
Brandon
  • 4,491
  • 6
  • 38
  • 59
0

I guess you talking about DragDrop. You can find example here: How to: Enable Drag-and-Drop Operations with the Windows Forms RichTextBox Control

Alex F
  • 3,180
  • 2
  • 28
  • 40
  • If he's talking about dragging the `PictureBox` itself, `DragDrop` is not the right event. That event is fired when something is dragged into the control and dropped, not when the control itself is dragged. – Brandon Mar 01 '16 at 13:46
  • @Brandon exactly. Can you help me? :) – Armandas Barkauskas Mar 01 '16 at 20:50