Drag and Drop

33
1 Drag and Drop (Not in the Textbook)

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 Presentation

Transcript of Drag and Drop

Page 1: Drag and Drop

1

Drag and Drop

(Not in the Textbook)

Page 2: Drag and Drop

2

Objectives

You will be able to use drag and drop in your Windows Forms applications.

Page 3: Drag and Drop

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

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

5

The Form

Page 6: Drag and Drop

6

Set Properties

Picture Boxes: Set BorderStyle Set SizeMode to Zoom

Button Enabled = False

Page 7: Drag and Drop

7

Add Event Handler

Display the events for pbSource.

Create an event handler for the MouseDown event

Page 8: Drag and Drop

8

Add Event Handler

Page 9: Drag and Drop

9

MouseDown Event Handler

private void pbSource_MouseDown(object sender, MouseEventArgs e)

{

pbSource.DoDragDrop(pbSource.Image,

DragDropEffects.Move);

}

Page 10: Drag and Drop

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

11

Form Load Event Handler

Page 12: Drag and Drop

12

Form Load Event Handler

private void Form1_Load(object sender, EventArgs e)

{

pbTarget.AllowDrop = true;

}

Page 13: Drag and Drop

13

More Event Handlers

Select pbTarget and display its events.

Create event handlers for DragEnter and DragDrop

Page 14: Drag and Drop

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

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

16

Program in Action

Click on pbSource and drag to pbTarget

Page 17: Drag and Drop

17

After Drag and Drop

Click on Reset Image

Page 18: Drag and Drop

18

Back to Original Form

Page 19: Drag and Drop

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

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

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

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

23

Multiple Sources in Action

Page 24: Drag and Drop

24

Multiple Sources in Action

Page 25: Drag and Drop

25

Multiple Targets

Add a second target, pbTarget2

Add DragEnter and DragDrop event handlers.

Page 26: Drag and Drop

26

Page 27: Drag and Drop

27

Allow Drop in pbTarget2

private void Form1_Load(object sender, EventArgs e)

{

pbTarget.AllowDrop = true;

pbTarget2.AllowDrop = true;

}

Page 28: Drag and Drop

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

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

30

Initial Form

Page 31: Drag and Drop

31

After Drag and Drop

Page 32: Drag and Drop

32

After Reset Image

End of Presentation

Page 33: Drag and Drop

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.