• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1301
  • Last Modified:

Drag-and-Drop multiple strings between listboxes

I need some help to implement the drag-and-drop related methods below. The idea is to select a set of items in one listbox and drag them all in one go to the other listbox. It shoud work the same way in both directions.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DragDropListBoxTest
{
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.ListBox listBox1;
            private System.Windows.Forms.ListBox listBox2;

            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  InitializeComponent();
            }

            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.listBox1 = new System.Windows.Forms.ListBox();
                  this.listBox2 = new System.Windows.Forms.ListBox();
                  this.SuspendLayout();
                  //
                  // listBox1
                  //
                  this.listBox1.AllowDrop = true;
                  this.listBox1.Items.AddRange(new object[] {
                                                                                      "First item",
                                                                                      "Second item",
                                                                                      "Third item"});
                  this.listBox1.Location = new System.Drawing.Point(16, 16);
                  this.listBox1.Name = "listBox1";
                  this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
                  this.listBox1.Size = new System.Drawing.Size(120, 95);
                  this.listBox1.TabIndex = 0;
                  this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
                  this.listBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
                  this.listBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox1_DragEnter);
                  //
                  // listBox2
                  //
                  this.listBox2.AllowDrop = true;
                  this.listBox2.Location = new System.Drawing.Point(152, 16);
                  this.listBox2.Name = "listBox2";
                  this.listBox2.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
                  this.listBox2.Size = new System.Drawing.Size(120, 95);
                  this.listBox2.TabIndex = 1;
                  this.listBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox2_MouseDown);
                  this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
                  this.listBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox2_DragEnter);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(288, 126);
                  this.Controls.Add(this.listBox2);
                  this.Controls.Add(this.listBox1);
                  this.Name = "Form1";
                  this.Text = "DragDropListBoxTest";
                  this.ResumeLayout(false);

            }
            #endregion

            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {

            }

            private void listBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {

            }

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

            }

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

            }

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

            private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
            {
            
            }
      }
}
0
liljegren
Asked:
liljegren
  • 2
1 Solution
 
Anandavally_PCommented:
Try this link
http://www.codeproject.com/cs/combobox/LarryDragAndDrop.asp

Thanks
Anandavally Perumal

0
 
liljegrenAuthor Commented:
Thank you. Sorry I haven't been able to evaluate this reply yet. I'll look into this more again in a few days. Then I'll post here again.
0
 
liljegrenAuthor Commented:
Yes, I'm sorry. I forgot about this. I'll accept Anandavally_P's answer now.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now