从列表框 2 dbedit 中拖放,使用逗号分隔值且没有重复项
Drag&Drop from listbox 2 dbedit with comma seperated values and no duplicates
我已将 listbox
属性设置为 dbautomatic
并将 multiselect
设置为 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;
我遇到的问题是,我只能得到一个值。我怎样才能获得多个值?
您将需要迭代 TListBox
中的所有项目并检查 Selected 属性。
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;
如果您想避免重复,请将每个项目添加到 TStringList
和 Sorted
属性 集。设置 Duplicates
属性 以满足您的需要并处理可能的异常。
我已将 listbox
属性设置为 dbautomatic
并将 multiselect
设置为 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;
我遇到的问题是,我只能得到一个值。我怎样才能获得多个值?
您将需要迭代 TListBox
中的所有项目并检查 Selected 属性。
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;
如果您想避免重复,请将每个项目添加到 TStringList
和 Sorted
属性 集。设置 Duplicates
属性 以满足您的需要并处理可能的异常。