drag and drop
DESCRIPTION
Drag and Drop. (Not in the Textbook). Objectives. You will be able to use drag and drop in your Windows Forms applications. Drag and Drop Example. Simple example of drag and drop. User will be able to drag an image from one picture box to another. Getting Started. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/1.jpg)
1
Drag and Drop
(Not in the Textbook)
![Page 2: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/2.jpg)
2
Objectives
You will be able to use drag and drop in your Windows Forms applications.
![Page 3: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/3.jpg)
3
Drag and Drop Example
Simple example of drag and drop.
User will be able to drag an image from one picture box to another.
![Page 4: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/4.jpg)
4
Getting Started
Create a new Windows Forms application. Drag_Drop_Example
Put two picture boxes and a button on the form, as shown on the next slide. Set BorderStyle Set SizeMode to Zoom
Set the image for the left hand picture box but not for the right hand one.
http://www.cse.usf.edu/~turnerr/Software_Systems_Development/Downloads/USF_Bull_small.jpg
![Page 5: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/5.jpg)
5
The Form
![Page 6: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/6.jpg)
6
Set Properties
Picture Boxes: Set BorderStyle Set SizeMode to Zoom
Button Enabled = False
![Page 7: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/7.jpg)
7
Add Event Handler
Display the events for pbSource.
Create an event handler for the MouseDown event
![Page 8: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/8.jpg)
8
Add Event Handler
![Page 9: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/9.jpg)
9
MouseDown Event Handler
private void pbSource_MouseDown(object sender, MouseEventArgs e)
{
pbSource.DoDragDrop(pbSource.Image,
DragDropEffects.Move);
}
![Page 10: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/10.jpg)
10
Form Load Event Handler
Select the form and display its events.
Create an event handler for the Load event.
![Page 11: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/11.jpg)
11
Form Load Event Handler
![Page 12: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/12.jpg)
12
Form Load Event Handler
private void Form1_Load(object sender, EventArgs e)
{
pbTarget.AllowDrop = true;
}
![Page 13: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/13.jpg)
13
More Event Handlers
Select pbTarget and display its events.
Create event handlers for DragEnter and DragDrop
![Page 14: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/14.jpg)
14
More Event Handlers
private void pbTarget_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pbTarget_DragDrop(object sender, DragEventArgs e)
{
DataObject dataObject = (DataObject) e.Data;
pbTarget.Image = (Image)dataObject.GetData(DataFormats.Bitmap);
pbSource.Image = null;
pbSource.Enabled = false;
btnResetImage.Enabled = true;
}
![Page 15: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/15.jpg)
15
Button Click Handler
Double click on the Reset Image button to create a click event handler.
private void btnResetImage_Click(object sender, EventArgs e)
{
pbSource.Image = pbTarget.Image;
pbSource.Enabled = true;
pbTarget.Image = null;
btnResetImage.Enabled = false;
}
Build and run
![Page 16: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/16.jpg)
16
Program in Action
Click on pbSource and drag to pbTarget
![Page 17: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/17.jpg)
17
After Drag and Drop
Click on Reset Image
![Page 18: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/18.jpg)
18
Back to Original Form
![Page 19: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/19.jpg)
19
Multiple Sources
Add another picture box, pbSource2. Set its Image property. http://www.cse.usf.edu/~turnerr/Software_Systems_Develo
pment/Downloads/Water_Lilies.jpg
Create a MouseDown event handlerprivate void pbSource2_MouseDown(object sender, MouseEventArgs e)
{
pbSource.DoDragDrop(pbSource2.Image,
DragDropEffects.Move);
}
![Page 20: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/20.jpg)
20
Remember the Source
public partial class Form1 : Form
{
PictureBox source;
....
private void pbSource_MouseDown(object sender, MouseEventArgs e)
{
source = pbSource;
pbSource.DoDragDrop(pbSource.Image,
DragDropEffects.Move);
}
private void pbSource2_MouseDown(object sender, MouseEventArgs e)
{
source = pbSource2;
pbSource2.DoDragDrop(pbSource2.Image,
DragDropEffects.Move);
}
![Page 21: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/21.jpg)
21
Prevent Another Drag
private void pbTarget_DragDrop(object sender, DragEventArgs e)
{
DataObject dataObject = (DataObject) e.Data;
pbTarget.Image = (Image)dataObject.GetData(DataFormats.Bitmap);
btnResetImage.Enabled = true;
source.Image = null;
pbSource.Enabled = false;
pbSource2.Enabled = false;
}
![Page 22: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/22.jpg)
22
Update Reset
private void btnResetImage_Click(object sender, EventArgs e)
{
if (pbSource.Image == null)
{
pbSource.Image = pbTarget.Image;
}
else
{
pbSource2.Image = pbTarget.Image;
}
pbTarget.Image = null;
pbSource.Enabled = true;
pbSource2.Enabled = true;
btnResetImage.Enabled = false;
}
![Page 23: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/23.jpg)
23
Multiple Sources in Action
![Page 24: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/24.jpg)
24
Multiple Sources in Action
![Page 25: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/25.jpg)
25
Multiple Targets
Add a second target, pbTarget2
Add DragEnter and DragDrop event handlers.
![Page 26: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/26.jpg)
26
![Page 27: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/27.jpg)
27
Allow Drop in pbTarget2
private void Form1_Load(object sender, EventArgs e)
{
pbTarget.AllowDrop = true;
pbTarget2.AllowDrop = true;
}
![Page 28: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/28.jpg)
28
pbTarget2 Event Handlers
private void pbTarget2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pbTarget2_DragDrop(object sender, DragEventArgs e)
{
DataObject dataObject = (DataObject)e.Data;
pbTarget2.Image = (Image)dataObject.GetData(DataFormats.Bitmap);
source.Image = null;
pbSource.Enabled = false;
pbSource2.Enabled = false;
btnResetImage.Enabled = true;
}
![Page 29: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/29.jpg)
29
Update Resetprivate void btnResetImage_Click(object sender, EventArgs e)
{
PictureBox current_target;
if (pbTarget.Image != null)
{
current_target = pbTarget;
}
else
{
current_target = pbTarget2;
}
if (pbSource.Image == null)
{
pbSource.Image = current_target.Image;
}
else
{
pbSource2.Image = current_target.Image;
}
current_target.Image = null;
pbSource.Enabled = true;
pbSource2.Enabled = true;
btnResetImage.Enabled = false;
}
![Page 30: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/30.jpg)
30
Initial Form
![Page 31: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/31.jpg)
31
After Drag and Drop
![Page 32: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/32.jpg)
32
After Reset Image
End of Presentation
![Page 33: Drag and Drop](https://reader035.vdocuments.us/reader035/viewer/2022062301/56814e8d550346895dbc3184/html5/thumbnails/33.jpg)
33
Assignment
Do this example for yourself if you haven't done it in class.
Study and understand how to do drag and drop. What properties to update on each
event.