Solved

Dual ListBox drag and drop

Posted on 2004-08-31
4
654 Views
Last Modified: 2012-06-21
Greetings,

I am attempting to implement a good old fashioned dual listbox dialog with the button to add one, add all, remove one and remove all. What I would like to implement is drag and drop between the two listboxes (with multiple selections if possible).

Please is there someone out there with an example I would be most grateful.

Thanks in advance.

Bonus points availble for quick and complete response (1000 up for grabs)
0
Comment
Question by:zaroblan
  • 2
4 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11943815
simple example ... uses treeviews and a list box.

http://www.c-sharpcorner.com/winforms/DragAndDrop2MG.asp
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11944012
also from the windows forms FAQ ..http://www.syncfusion.com/FAQ/WinForms/FAQ_c87c.asp

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

namespace DragDrop
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private DragDrop.Form1.ListBoxDragNDrop listBox1;
            private DragDrop.Form1.ListBoxDragNDrop listBox2;

            private System.Windows.Forms.Control dropSource = null;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            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 DragDrop.Form1.ListBoxDragNDrop();
                  this.listBox2 = new DragDrop.Form1.ListBoxDragNDrop();
                  this.SuspendLayout();
                  //
                  // listBox1
                  //
                  this.listBox1.AllowDrop = true;
                  this.listBox1.Items.AddRange(new object[] {
                                                                                      "round",
                                                                                      "square",
                                                                                      "thin"});
                  this.listBox1.Location = new System.Drawing.Point(96, 16);
                  this.listBox1.Name = "listBox1";
                  this.listBox1.Size = new System.Drawing.Size(200, 225);
                  this.listBox1.TabIndex = 0;
                        //
                  // listBox2
                  //
                  this.listBox2.AllowDrop = true;
                  this.listBox2.Items.AddRange(new object[] {
                                                                                      "red",
                                                                                      "blue",
                                                                                      "green"});
                  this.listBox2.Location = new System.Drawing.Point(384, 16);
                  this.listBox2.Name = "listBox2";
                  this.listBox2.Size = new System.Drawing.Size(200, 225);
                  this.listBox2.TabIndex = 1;
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(672, 301);
                  this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                                              this.listBox2,
                                                                                                              this.listBox1});
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            public class ListBoxDragNDrop : ListBox
            {
                  private int lastMouseUpItemIndex = -1;
                  private bool isDropSource = false;
                  private int lastLine = -1;

                  public ListBoxDragNDrop()
                  {
                        this.AllowDrop = true;
                        this.SelectionMode = SelectionMode.One;
                        DragDrop += new System.Windows.Forms.DragEventHandler(OnDragDrop);
                        DragEnter += new System.Windows.Forms.DragEventHandler(OnDragEnter);
                        DragLeave += new System.EventHandler(OnDragLeave);
                        MouseDown += new System.Windows.Forms.MouseEventHandler(OnMouseDown);
                        DragOver += new System.Windows.Forms.DragEventHandler(OnDragOver);
                        SelectedIndexChanged += new System.EventHandler(OnSelectedIndexChanged);
                  }
                  
                  private void DrawVisualQue(Color color, ref Rectangle rectangle)
                  {
                        Graphics g = Graphics.FromHwnd(this.Handle);
                         g.DrawLine(new Pen(color), rectangle.X, lastLine, rectangle.Width, lastLine);
                   }

                  private void OnDragLeave(object sender, EventArgs e)
                  {
                        if(lastLine > -1)
                        {
                              Rectangle rect = this.GetItemRectangle(0);
                              DrawVisualQue(Color.White, ref rect);
                        }
                  }
                  
                  private void OnDragOver(object sender, DragEventArgs e)
                  {
                        if(e.Effect == DragDropEffects.Copy)
                        {
                              Point point = this.PointToClient(new Point(e.X, e.Y));
                              int index = this.IndexFromPoint(point);
                              bool belowLastItem = false;
                              if(index < 0 || index >= this.Items.Count)
                              {
                                    index = this.Items.Count - 1;
                                    belowLastItem = true;
                              }
                              Rectangle rect = this.GetItemRectangle(index);
                              if(lastLine > -1)
                                    DrawVisualQue(Color.White, ref rect);
                              lastLine = rect.Y + (belowLastItem ? rect.Height : 0);
                              DrawVisualQue(Color.Red, ref rect);
                        }            
                  }
                  
                  private void OnDragDrop(object sender, DragEventArgs e)
                  {
                        if(e.Effect == DragDropEffects.Copy)
                        {
                              Point point = this.PointToClient(new Point(e.X, e.Y));
                              int index = this.IndexFromPoint(point);
                              if( index > -1 && index < this.Items.Count)
                                    Items.Insert(index, e.Data.GetData(DataFormats.Text));
                              else
                                    index = Items.Add(e.Data.GetData(DataFormats.Text));
                              
                              this.SelectedIndex = index;
                        }            
                  }

                  private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
                  {
                        if (e.Data.GetDataPresent(DataFormats.Text) && !isDropSource )
                              e.Effect = DragDropEffects.Copy;
                        else
                              e.Effect = DragDropEffects.None;

                        lastLine = -1;
                  }

                  private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
                  {
                        if(MouseButtons == MouseButtons.Left
                              && SelectedIndex == lastMouseUpItemIndex
                              && SelectedItem != null)
                        {
                              isDropSource = true;
                              DoDragDrop(SelectedItem, DragDropEffects.Copy);
                              isDropSource = false;
                              lastMouseUpItemIndex = this.SelectedIndex;
                        }
                  }

                  private void OnSelectedIndexChanged(object sender, System.EventArgs e)
                  {
                        lastMouseUpItemIndex = this.SelectedIndex;
                  }
            }
      }
}
0
 
LVL 19

Accepted Solution

by:
Desp earned 500 total points
ID: 11944031

See the example given in this Article thats shows how to drag n drop between 2 listboxes:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsControlClassDoDragDropTopic.asp
0
 

Author Comment

by:zaroblan
ID: 11950668
I did not expect this to be so complicated.
My users will have to do without drag and drop ... for now :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now