Results 1 to 10 of 10

Thread: Drag & Drop in Windows Forms

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Drag & Drop in Windows Forms

    VB version here.

    I'm sure others have posted about this topic before but I'm sure another won't hurt. I'll post various examples of drag & drop within the same control, between controls and between applications.

    If you're interested in performing drag & drop operations in WinForms then I suggest that you read this and this at least.

    I'll start with moving items between two ListBoxes, which is relatively simple.

    1. Create a new WinForms application project.
    2. Add two ListBoxes to the form.
    3. Replace the existing form code with the following:
    Code:
    public Form1()
    {
        InitializeComponent();
    
        // Hook up the form event handler.
        this.Load += new EventHandler(Form1_Load);
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        // Allow data to be dropped on both ListBoxes.
        this.listBox1.AllowDrop = true;
        this.listBox2.AllowDrop = true;
    
        // Populate the ListBoxes.
        this.listBox1.Items.AddRange(new string[] {"List 1, Item 1",
                                                   "List 1, Item 2",
                                                   "List 1, Item 3",
                                                   "List 1, Item 4",
                                                   "List 1, Item 5"});
        this.listBox2.Items.AddRange(new string[] {"List 2, Item 1",
                                                   "List 2, Item 2",
                                                   "List 2, Item 3",
                                                   "List 2, Item 4",
                                                   "List 2, Item 5"});
    
        // Hook up the ListBox event handlers.
        this.listBox1.MouseDown += new MouseEventHandler(ListBox_MouseDown);
        this.listBox2.MouseDown += new MouseEventHandler(ListBox_MouseDown);
        this.listBox1.DragEnter += new DragEventHandler(ListBox_DragEnter);
        this.listBox2.DragEnter += new DragEventHandler(ListBox_DragEnter);
        this.listBox1.DragDrop += new DragEventHandler(ListBox_DragDrop);
        this.listBox2.DragDrop += new DragEventHandler(ListBox_DragDrop);
    }
    
    void ListBox_MouseDown(object sender, MouseEventArgs e)
    {
        ListBox source = (ListBox)sender;
    
        for (int index = 0; index < source.Items.Count; index++)
        {
            // Test whether the mouse location is within an item
            if (source.GetItemRectangle(index).Contains(e.Location))
            {
                // The mouse was depressed on an item so allow a move operation.
                source.DoDragDrop(source, DragDropEffects.Move);
    
                break;
            }
        }
    }
    
    void ListBox_DragEnter(object sender, DragEventArgs e)
    {
        ListBox source = (ListBox)sender;
    
        if (e.Data.GetDataPresent("System.Windows.Forms.ListBox", false) &&
            e.Data.GetData("System.Windows.Forms.ListBox", false) != source)
        {
            // The data being dragged is a ListBox but not the one that was just entered.
            e.Effect = DragDropEffects.Move;
        }
    }
    
    void ListBox_DragDrop(object sender, DragEventArgs e)
    {
        ListBox source = (ListBox)sender;
    
        if (e.Data.GetDataPresent("System.Windows.Forms.ListBox", false))
        {
            // Get the ListBox that the data was dragged from.
            ListBox data = (ListBox)e.Data.GetData("System.Windows.Forms.ListBox", false);
    
    
            // Make sure we aren't trying to drag from and drop to the same control.
            if (data != source)
            {
                // Get the item that was dragged.
                object item = data.SelectedItem;
    
                // Remove the item from its original location.
                data.Items.Remove(item);
    
                // Get the current mouse location relative to the control being dropped on.
                Point location = source.PointToClient(new Point(e.X, e.Y));
                int dropIndex = -1;
    
                // Find the item over which the mouse was released.
                for (int index = 0; index < source.Items.Count; index++)
                {
                    if (source.GetItemRectangle(index).Contains(location))
                    {
                        dropIndex = index;
    
                        break;
                    }
                }
    
                if (dropIndex == -1)
                {
                    // The mouse was not released over an item so add the new item to the end.
                    source.Items.Add(item);
                }
                else
                {
                    // Insert the new item above the item it was dropped on.
                    source.Items.Insert(dropIndex, item);
                }
            }
        }
    }
    4. Run the project and start dragging and dropping.

    I'll refine this example and add more over time. If there's anything you specifically want to see then post a request and I'll see if I can get to it.
    Last edited by jmcilhinney; Nov 15th, 2008 at 04:47 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width