Schlagwort-Archive: DoubleClick

TListBox no double click if item count is zero

There is a „problem“ with TListBox and the DblClick event. It doesn’t fire if the ListBox has no items. Steps to reproduce:

  • create a new vcl forms application
  • drop a TListBox on that form
  • assign the DblClick Event
  • write the following code
procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
  ShowMessage('ListBox1DblClick');
end;
  • compile and run the application

If You can see the message doesn’t appear. Stop the application, add an item to the ListBox, compile and run again. Now the message is shown. This behavior is different to TListView. A TListView double click is independent from the items property. If You want to have a double click on an empty TListBox You can use the OnMouseDown event in combination with TShiftState like this:

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (ssLeft in Shift) and (ssDouble in Shift) then
    ShowMessage('ListBox1MouseDown -> DoubleClick');
end;