2013-06-26 55 views
0

我有一個DragMode設置爲dmAutomatic的列表框,可以將項目的文本拖到其他地方。Drag and MouseDown conflict

我將Multiselect設置爲true。我希望能夠點擊並拖動我的列表框項目以按順序選擇多行。所以我有:

Shift := [ssLeft]; 

ListBoxMouseDown事件。拖動多選不起作用。如果我在按住Shift鍵的同時按住Shift鍵,我會得到期望的結果。有關爲什麼或如何解決此問題的任何建議?

回答

0

你正在走這個錯誤的路。根本不要使用DragMode=dmAutomatic,這並不意味着你如何使用它。相反,在OnMouseDown事件中,當左按鈕關閉時設置一個標誌。在OnMouseMove事件中,如果標誌已設置,請選擇鼠標下的當前項目。在OnMouseUp事件中,清除標誌。例如:

var 
    Dragging: Boolean = False; 

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
begin 
    If Button = mbLeft then 
    Dragging := True; 
end; 

procedure TForm1.ListBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
begin 
    If Button = mbLeft then 
    Dragging := False; 
end; 

procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); 
var 
    Index: Integer; 
begin 
    If not Dragging then Exit; 
    Index := ListBox1.ItemAtPos(Point(X, Y), True): Integer; 
    If Index <> -1 then 
    ListBox1.Selected[Index] := True; 
end;