[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 856
  • Last Modified:

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.
0
966
Asked:
966
  • 6
  • 3
1 Solution
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now