SIGN UP MEMBER LOGIN:    
ARTICLE

Drag-and-Drop Operation in Windows Forms and C#

Posted by Hirendra Sisodiya Articles | Windows Forms C# April 24, 2010
This article describes about Drag-and-Drop Operation in Windows Forms and C#.
Reader Level:
Download Files:
 

In windows application for performing drag and drop operation we need to understand working of DragEnter, DragDrop, MouseDown  and Dragleave events. With the help of these events we can perform drag and drop functionality very easily.


There are two steps first is dragging and second is dropping.


For better understanding we can take an example:


In this example there are two list box on window from names with ListBoxA and ListBoxB , ListBoxA contains  some city's name New York, New Delhi, Bombay, London, Paris, Berlin etc..

 

Step3.JPG


Now we have to drag item from ListBoxA and Drop to ListBoxB


We can divide all task in two steps


Step 1:  Perform Drag Operation


First the MouseDown event is used to start the drag operation.
 
In the MouseDown event for the control where the drag will begin, use the DoDragDrop method to set the data to be dragged and the allowed effect dragging will have.

 

private void listBoxA_MouseDown(object sender, MouseEventArgs e)

{

    listBoxA.DoDragDrop(listBoxA.SelectedItem, DragDropEffects.Copy)

}

 

Step 2:  Perform Drop Operation

  1. Set the AllowDrop property of ListBoxB  to true.

  2. In the DragEnter event for the ListBoxB  where the drop will occur, ensure that the data being dragged is of an acceptable type (in this case of Listbox- Text). The code then sets the effect that will happen when the drop occurs to a value in the DragDroeffects enumeration that has these option:

    All
    Copy
    Link
    Move
    Scroll

    private
    void listBoxB_DragEnter(object sender, DragEventArgs e)
    {
        
    if (e.Data.GetDataPresent(DataFormats.Text))
            e.Effect =
    DragDropEffects.Copy;
        
    else
            e.Effect = DragDropEffects.None;


  3. In the DragDrop event for the control where the drop will occur, use the GetData method to retrieve the data being dragged.

    private
    void listBoxB_DragDrop(object sender, DragEventArgs e)
    {
        listBox2.Items.Add(e.Data.GetData(
    DataFormats.Text).ToString());
    }

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks you very much !

Posted by Huy Vu Sep 16, 2010

 how to save data from windows form to database?? ....hmmmm
sorry kamlash, i didn't understand your question
so please explain in brief that what you want?

Posted by Hirendra Sisodiya Aug 09, 2010

how to save data from windows form to database using drag and drop operation


reply as soon as possible its very urgent please...

Posted by Kamlesh Kumar Sati Aug 09, 2010

Check your mail...

Posted by Hirendra Sisodiya Jul 13, 2010

would you mind examine my c# source? please, help me.
i don't know why don't working drag & drop my c# source
if you send e-mail me , i will send my source that i don't understand.

thebest0527@naver.com  <- i'll waiting your message

Posted by kim sung jin Jul 12, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor