zaroblan
asked on
Dual ListBox drag and drop
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)
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)
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.ListBoxDrag NDrop listBox1;
private DragDrop.Form1.ListBoxDrag NDrop listBox2;
private System.Windows.Forms.Contr ol dropSource = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont ainer 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.ListBoxDrag NDrop();
this.listBox2 = new DragDrop.Form1.ListBoxDrag NDrop();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.AllowDrop = true;
this.listBox1.Items.AddRan ge(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.AddRan ge(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.Contr ol[] {
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.DragE ventHandle r(OnDragDr op);
DragEnter += new System.Windows.Forms.DragE ventHandle r(OnDragEn ter);
DragLeave += new System.EventHandler(OnDrag Leave);
MouseDown += new System.Windows.Forms.Mouse EventHandl er(OnMouse Down);
DragOver += new System.Windows.Forms.DragE ventHandle r(OnDragOv er);
SelectedIndexChanged += new System.EventHandler(OnSele ctedIndexC hanged);
}
private void DrawVisualQue(Color color, ref Rectangle rectangle)
{
Graphics g = Graphics.FromHwnd(this.Han dle);
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(inde x);
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(D ataFormats .Text));
this.SelectedIndex = index;
}
}
private void OnDragEnter(object sender, System.Windows.Forms.DragE ventArgs e)
{
if (e.Data.GetDataPresent(Dat aFormats.T ext) && !isDropSource )
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
lastLine = -1;
}
private void OnMouseDown(object sender, System.Windows.Forms.Mouse EventArgs e)
{
if(MouseButtons == MouseButtons.Left
&& SelectedIndex == lastMouseUpItemIndex
&& SelectedItem != null)
{
isDropSource = true;
DoDragDrop(SelectedItem, DragDropEffects.Copy);
isDropSource = false;
lastMouseUpItemIndex = this.SelectedIndex;
}
}
private void OnSelectedIndexChanged(obj ect sender, System.EventArgs e)
{
lastMouseUpItemIndex = this.SelectedIndex;
}
}
}
}
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.ListBoxDrag
private DragDrop.Form1.ListBoxDrag
private System.Windows.Forms.Contr
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
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.ListBoxDrag
this.listBox2 = new DragDrop.Form1.ListBoxDrag
this.SuspendLayout();
//
// listBox1
//
this.listBox1.AllowDrop = true;
this.listBox1.Items.AddRan
"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.AddRan
"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
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.DragE
DragEnter += new System.Windows.Forms.DragE
DragLeave += new System.EventHandler(OnDrag
MouseDown += new System.Windows.Forms.Mouse
DragOver += new System.Windows.Forms.DragE
SelectedIndexChanged += new System.EventHandler(OnSele
}
private void DrawVisualQue(Color color, ref Rectangle rectangle)
{
Graphics g = Graphics.FromHwnd(this.Han
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,
}
}
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(inde
if(lastLine > -1)
DrawVisualQue(Color.White,
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
else
index = Items.Add(e.Data.GetData(D
this.SelectedIndex = index;
}
}
private void OnDragEnter(object sender, System.Windows.Forms.DragE
{
if (e.Data.GetDataPresent(Dat
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
lastLine = -1;
}
private void OnMouseDown(object sender, System.Windows.Forms.Mouse
{
if(MouseButtons == MouseButtons.Left
&& SelectedIndex == lastMouseUpItemIndex
&& SelectedItem != null)
{
isDropSource = true;
DoDragDrop(SelectedItem, DragDropEffects.Copy);
isDropSource = false;
lastMouseUpItemIndex = this.SelectedIndex;
}
}
private void OnSelectedIndexChanged(obj
{
lastMouseUpItemIndex = this.SelectedIndex;
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did not expect this to be so complicated.
My users will have to do without drag and drop ... for now :)
My users will have to do without drag and drop ... for now :)
http://www.c-sharpcorner.com/winforms/DragAndDrop2MG.asp