Click here to Skip to main content
15,886,026 members
Articles / Desktop Programming / Windows Forms

Drag-and-Drop ListBox

Rate me:
Please Sign up or sign in to vote.
4.93/5 (33 votes)
18 May 2009CPOL14 min read 191.9K   7.3K   72  
DragDropListBox, a control derived from ListBox allowing drag-and-drop in multiselect mode.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Oli.Controls.Tests
{
	public partial class TestDragDropListBox : UserControl
	{
		public TestDragDropListBox()
		{
			InitializeComponent();
			dragDropListBoxBindingSource.DataSource = dragDropListBox;
			cboSelectionMode.DataSource = Enum.GetNames(typeof(SelectionMode));
			cboSelectionMode.Text = "MultiExtended";
		}

		public void AddItemsRange(object[] items)
		{
			dragDropListBox.Items.AddRange(items);
		}

		[Category("Behavior (drag-and-drop)"),
		 DefaultValue(""),
		 Description("Drag-and-drop group to which the DragDropListBox belongs. Drag-and-drop is restricted to happen between DragDropListBoxes having the same DragDropGroup.")]
		public string DragDropGroup
		{
			get { return dragDropListBox.DragDropGroup; }
			set { txtGroup.Text = value; dragDropListBox.DragDropGroup = value; }
		}

		private void cboSelectionMode_SelectedIndexChanged(object sender, EventArgs e)
		{
			dragDropListBox.SelectionMode = (SelectionMode)Enum.Parse(typeof(SelectionMode), cboSelectionMode.Text);
		}

		private void btnSelectEven_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < dragDropListBox.Items.Count; i++) {
				dragDropListBox.SetSelected(i, i % 2 == 0);
			}
		}

		private void chkSorted_CheckedChanged(object sender, EventArgs e)
		{
			dragDropListBox.Sorted = chkSorted.Checked; // DataBinding doesn't work well (list isn't sorted immediately).
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer CySoft
Switzerland Switzerland
Apprenticeship as machine draughtsman.
School of engineering in electrical engineering.
Started programming as hobbyist. After some years as a programmer in the industry, I have made myself independent.

I speak: French, German, (some) English.

Languages: C#, VB.NET, Basic, Pascal, Modula-2, Oberon-2, VBA.

Databases: FoxPro, MS-Access, SQL-Server, Oracle.

Hobbies: Astronomy, space, science fiction, travel, nature.

Comments and Discussions