Recent

Author Topic: Drag And Drop with Two ListBox  (Read 816 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Drag And Drop with Two ListBox
« on: November 24, 2020, 03:50:56 pm »
Hey Guys

I Have 2 ListBox.

The First 1 Got Accessories witch can be Selected by Drag Droping them into the Second 1,

is There Any Way to Allso make expand it to a DblClick Event.

For Example if u DblClick any item in the Left Box it will appear in the Right(The Second one)

Thanks :)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Drag And Drop with Two ListBox
« Reply #1 on: November 24, 2020, 05:18:44 pm »
Use the OnDblClick event. This, for example, is from my "player".

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.ListDblClick(Sender: TObject);
  2. { Double clicking a filename in the list plays it inmediately }
  3. begin
  4.   if FileList.ItemIndex >= 0 then
  5.     PlaySong(FileList.ItemIndex);
  6. end;

FileList is a TListBox and this is its OnDblClick handler. We first check whether there's any element selected and if so, call one of two overloads which accepts a "song" index and calls the other overload with the corresponding item string (a file name):

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.PlaySong(const Index: Integer);
  2. { Plays the file whose index in the list is Index }
  3. begin
  4.   if (Index >= 0) and (Index < FileList.Count) then begin
  5.     Current := Index;
  6.     PlaySong(FileList.Items[Index]);
  7.   end;
  8. end;

In your case you'd do something like:

Code: Pascal  [Select][+][-]
  1. SecondList.Items.Add(FirstList.Items[FileList.ItemIndex])

HTH!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Drag And Drop with Two ListBox
« Reply #2 on: November 24, 2020, 05:42:22 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Forms, StdCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     LeftListBox: TListBox;
  13.     RightListBox: TListBox;
  14.     procedure FormCreate(Sender: TObject);
  15.     procedure LeftListBoxDblClick(Sender: TObject);
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.lfm}
  24.  
  25. { TForm1 }
  26.  
  27. procedure TForm1.FormCreate(Sender: TObject);
  28. var
  29.   i: Integer;
  30. begin
  31.   for i := 0 to 7 do
  32.     LeftListBox.Items.Add('Item # %d', [i]);
  33. end;
  34.  
  35. procedure TForm1.LeftListBoxDblClick(Sender: TObject);
  36. begin
  37.   if LeftListBox.ItemIndex > -1 then
  38.     RightListBox.Items.Add(LeftListBox.Items[LeftListBox.ItemIndex]);
  39. end;
  40.  
  41. end.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Drag And Drop with Two ListBox
« Reply #3 on: November 25, 2020, 10:24:17 am »
Thank you Guys :)

Now I know my next Project too. A Music player sounds good :D
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

 

TinyPortal © 2005-2018