Search code examples
delphidelphi-7

Drag&Drop from listbox 2 dbedit with comma seperated values and no duplicates


I have made my listbox properties to dbautomatic and multiselect to true.

procedure TForm2.Edit1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := True;
end;

procedure TAuftragsVerteilewaehlen.Edit1DragDrop(Sender, Source: TObject; X,
          Y: Integer);
begin
         (Sender as TEdit).Text := (Source as TListBox).Items [(Source as TListBox).ItemIndex]
        end;

The problem I have is, I could get only one value. How could i get multiple values?


Solution

  • You will need to iterate all items in the TListBox and check the Selected property.

    s := '';
    for i := 0 to (Source as TListBox).Items.Count - 1 do 
      if (Source as TListBox).Selected[i] then  // Do your stuff with Item
        s := s + (Source as TListBox).Items[i] + ',';
    SetLength(s,Length(s)-1);
    (Sender as TEdit).Text := s;
    

    If you want to avoid duplicates, add each item to a TStringList with Sorted property set. Set the Duplicates property to fit your needs and handle possible exceptions.