Drag and Drop in C#.NET. How to implement drag and drop (Draganddrop) using c# or any .NET language.

DRAG AND DROP C#

C# Tutorials

  

DRAG AND DROP C#

Learn Tutorials
Drag and Drop in C#.NET. How to implement drag and drop (Draganddrop) using c# or any .NET language.

Example code for Drag and drop C#:
Drag and Drop summary

1) make sure the Main function is marked with STAThread Attribute:
   [STAThread]
   static void Main()
   {
      Application.Run(new Form1());
   }


2) DRAG SOURCE: call DoDragDrop in the MouseDown event (to allow initiating of a drag drop operation in the drag source)

   private void pictureBox1_MouseDown(object sender,
                     System.Windows.Forms.MouseEventArgs e)
   {
      // first parameter is what gets made available to drop target
      // second parameter is any combination of effects you want to support
         DoDragDrop(currentShowingContentID,DragDropEffects.Copy);
   }


3) DROP TARGET: set the AllowDrop property of drop target control to true


4) DROP TARGET: make the drop target subscribe to DragEnter event (so it will be able to accept data); specify what data will accpet by setting the Effect

   private void button_DragEnter(object sender,
                     System.Windows.Forms.DragEventArgs e)
   {

    //GetDataPresent|GetData|GetFormats; typeof any from predefined list below
      
      if(e.Data.GetDataPresent(typeof(int)))
          //or GetDataPresent(DataFormats.FileDrop) for example
      {
         //Copy|Move|Link|Scroll|All|None
         e.Effect = DragDropEffects.Copy;
         Button thisButton=(Button)sender;
         thisButton.BackColor=Color.DarkBlue;
      }
      else
         e.Effect = DragDropEffects.None;
   }


5) DROP TARGET: implement DragDrop event (to handle the actual drop in the drop target)

   private void button_DragDrop(object sender,
                  System.Windows.Forms.DragEventArgs e)
   {
      int contentIDdropped=(int)e.Data.GetData(typeof(int));
   }




predefined static data formats:

Bitmap
CommaSeparatedValue
Dib
Dif
EnhancedMetafile
FileDrop
Html
Locale
MetafilePict
OemText
Palette
PenData
Riff
Rtf
Serializable
StringFormat
SymbolicLink
Text
Tiff
UnicodeText
WaveAudio
This is example code for drag and drop in c#.net. how to implement drag and drop (draganddrop) using c# or any .net language.