Lazarus

Programming => LCL => Topic started by: Weitentaaal on November 24, 2020, 03:50:56 pm

Title: Drag And Drop with Two ListBox
Post by: Weitentaaal 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 :)
Title: Re: Drag And Drop with Two ListBox
Post by: lucamar 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!
Title: Re: Drag And Drop with Two ListBox
Post by: howardpc 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.
Title: Re: Drag And Drop with Two ListBox
Post by: Weitentaaal on November 25, 2020, 10:24:17 am
Thank you Guys :)

Now I know my next Project too. A Music player sounds good :D
TinyPortal © 2005-2018