1. Computing & Technology

Discuss in my forum

List Box with Drag and Drop Exposing the OnChange Event

By , About.com Guide

list box enhanced

list box enhanced

Code submitted by Jens Borrisholt

Delphi's TListBox control displays a collection of items in a scrollable list that users can select, add, or delete.

The "standard" implementation does not provide an event you can handle when the selected items gets changed in the list box - there's no OnChange event to handle.

Also, while implementing drag and drop functionality for two list boxes is not too complex it might be tricky for a beginner developer.

To overcome the above missing features of the TListBox, Jens Borrisholt submitted his own implementation of an enhanced list box implementation using the interceptor class idea.

TListBox.OnChange; TListBox.DragDropListBox; TListBox.AllowInternalDrag

The demo project you can download shows how to use the properties addded to the list box.
 begin
   //Add dummy items to the list box
   while ListBox1.Items.Count < 20 do
     ListBox1.Items.Add(IntToStr(ListBox1.Items.Count)) ;
 
   //assign a onchanve event to your ListBox
   ListBox1.OnChange := ListBox1Change;
 
   //If you only want to drag items in you own listbox
   //ListBox1.DragDropListBox := ListBox1;
 
 
   //If you only want to drag between two listboxes
   ListBox1.DragDropListBox := ListBox2;
 
   //do now allow items do be dragged within the Listbox
   ListBox1.AllowInternalDrag := false;
 end;
 

©2012 About.com. All rights reserved.

A part of The New York Times Company.