Drag and Drop Using Class Method

I have a range of drag and drop actions that work great on my quiz. (From answering questions to choosing an avatar)

There are many repetitions of the code, and I think it will be better implemented from the class and will call the method every time I use drag and drop.

Firstly, can this be done? and secondly, do I need a new drag and drop method?

Any thoughts or gross ideas would be great.

private void pictureBox2_MouseDown(object sender, MouseEventArgs e) { pictureBox2.DoDragDrop(pictureBox2.Image, DragDropEffects.Copy); } private void panel1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } private void panel1_DragDrop(object sender, DragEventArgs e) { //Set background image of panel to selected avatar' panel1.BackgroundImage = (Image)e.Data.GetData(DataFormats.Bitmap); } 
+1
c # winforms drag-and-drop
source share
1 answer

If you want to avoid duplicating the same events of several controls, you should use common events:

 private void commonPBox_MouseDown(object sender, MouseEventArgs e) { PictureBox PB = sender as PictureBox; if (PB == null) return; //or throw an exception PB.DoDragDrop(PB.Image, DragDropEffects.Copy); } private void commonPanel_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } private void commonPanel_DragDrop(object sender, DragEventArgs e) { Panel Pan = sender as Panel; if (Pan == null) return; //or throw an exception //Set background image of panel to selected avatar Pan.BackgroundImage = (Image)e.Data.GetData(DataFormats.Bitmap); } 

Select the appropriate controls and enter the event names in the corresponding event name slots in the event panel of the property tab.

Notice how I applied the event sender parameter to get a reference to the type of control that triggers the event. If the cast is not true, the reference is set to null .

If you want more control and flexibility, you might consider creating a DragAndDropcontroller .. class:

 static class DnDCtl { static List<Control> Targets = new List<Control>(); static List<Control> Sources = new List<Control>(); static public void RegisterSource(Control ctl) { if (!Sources.Contains(ctl) ) { Sources.Add(ctl); ctl.MouseDown += ctl_MouseDown; } } static public void UnregisterSource(Control ctl) { if (Sources.Contains(ctl)) { Sources.Remove(ctl); } } static public void RegisterTarget(Control ctl) { if (!Targets.Contains(ctl)) { Targets.Add(ctl); ctl.DragEnter += ctl_DragEnter; ctl.DragDrop += ctl_DragDrop; ctl.AllowDrop = true; } } static public void UnregisterTarget(Control ctl) { if (Targets.Contains(ctl)) { Targets.Remove(ctl); ctl.DragEnter -= ctl_DragEnter; ctl.DragDrop -= ctl_DragDrop; } } static void ctl_MouseDown(object sender, MouseEventArgs e) { PictureBox PB = sender as PictureBox; if (PB != null) PB.DoDragDrop(PB.Image, DragDropEffects.Copy); Panel Pan = sender as Panel; if (Pan != null) Pan.DoDragDrop(Pan.BackgroundImage, DragDropEffects.Copy); } static void ctl_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } static void ctl_DragDrop(object sender, DragEventArgs e) { Panel Pan = sender as Panel; if (Pan != null) Pan.BackgroundImage = (Image)e.Data.GetData(DataFormats.Bitmap); PictureBox PB = sender as PictureBox; if (PB != null) PB.BackgroundImage = (Image)e.Data.GetData(DataFormats.Bitmap); } } 

Notes:

  • I encoded symmetrical actions for Panels and PictureBoxes to show how you can handle various controls.
  • I created, but did not use Lists sources and goals. For more complex projects, you will find them useful.
  • I encoded the Register and Unregister methods. You can register after a certain condition and cancel the registration when it is no longer applicable.
  • Such a controller is good for dynamically using or disabling Drag & Drop for controls, especially. when you create them dynamically.
  • You can also pass it to delegates to separate the dragdrop action from the controller.

Also note that some people sometimes prefer to use the MouseMove through MouseDown esp. as it’s not so easy to make a choice.

ListView has a special event instead of ListView.ItemDrag , which obviously should be used when registering!

+2
source share

All Articles