How to move an item from listbox 1 to listbox 2

How do i move an item saying "bbb" from listbox 1 to listbox 2 ?

making sure the item in listbox 1 has been cleared, as it now exists in listbox 2.

thanks, thats it.
966Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thiagoblimeiraCommented:
Read this article, it explains how to do it with commented source code

http://www.delphicorner.f9.co.uk/articles/comps6.htm
0
thiagoblimeiraCommented:
another good article with source code

http://www.delphi-central.com/drag.aspx
0
966Author Commented:
i dont want the user to drag n drop.

when i press button 1 i want "bbb" to be cleared from listbox 1 and go into listbox 2.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

thiagoblimeiraCommented:
unit Unit1;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls;



type

TForm1 = class(TForm)

ListBox1: TListBox;

ListBox2: TListBox;

procedure ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;

State: TDragState; var Accept: Boolean);

procedure ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);

procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

private

{ Private declarations }

public

{ Public declarations }

end;



var

Form1: TForm1;



implementation



{$R *.DFM}



procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;

State: TDragState; var Accept: Boolean);

begin

Accept := (Source is TListBox);

end;



procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);

var

I, N : Integer;

Found : Boolean;

begin

with Source as TListBox do

 begin

for I := 0 to Items.Count - 1 do

if Selected[I] then begin

Found := False;

for N := 0 to (Sender AS TListBox).Items.Count - 1 do

if (Sender AS TListBox).Items[N] = Items[I] then

Found := True;



if NOT Found then

(Sender AS TListBox).Items.Add(Items[I]);

end;

end;

end;



procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

if ssShift in Shift then

ListBox1.BeginDrag(True);

end;



end.



That code will show you how to drag items from Listbox1 to Listbox2. *You can even have MultiSelect turned on. *This code shows you how to make them press the Shift key before they can drag.



The code above copies the string to the other listbox. *If you wanted to just move it over and have it disappear in ListBox1, then try this.



procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);

var

I : Integer;

Found : Boolean;

begin

with (Source AS TListBox) do begin

for I := 0 to Items.Count - 1 do

if Selected[I] then

(Sender AS TListBox).Items.Add(Items[I]);



for I := Items.Count - 1 downto 0 do

if Selected[I] then

Items.Delete(I);

end;

end;
0
thiagoblimeiraCommented:
that's it

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox2.Items.Add(ListBox1.Items[ListBox1.ItemIndex]);
ListBox1.DeleteSelected;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
966Author Commented:
when i press button 1 what do i put in the code to have it delete "bbb" and put it into listbox2
0
966Author Commented:
thnx but i dont want to highlight it. just for it to do it.

possible?
0
thiagoblimeiraCommented:
ListBox2.Items.Add(ListBox1.Items[{place the bbb text itemindex here}]);
ListBox1.Items.Delete[{place the bbb text itemindex here}] ;
0
thiagoblimeiraCommented:
a better solution


it searchs for the string index

ListBox2.Items.Add(ListBox1.Items[ListBox1.Items.IndexOf('bbb')]);
ListBox1.Items.Delete(ListBox1.Items.IndexOf('bbb'));
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.