0

In Delphi 2007 I'm attempting for force a TComboBox to drop down, however when I do, the mouse cursor disappears while the control is editing and the the mouse is over the form. I've come upon this idea after reading this SO answer on implementing a filter feature using a TComboBox.

To demonstrate the behavior.

  1. Run the sample code below (time machine might be needed to 2007)
  2. click in the combo box
  3. Start typing.

In the sample code you can see two workarounds I tried that did not prove fruitful.

Can the disappearing cursor be overcome?

An alternative free control/component that achieves the same goal of filtering a list while typing is welcomed as well. I need to filter items based on if they contain the text anywhere within the item, so the autocomplete route doesn't appear to be an option.

Cheers,

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure ComboChange(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.AutoComplete := false;
  ComboBox1.Style := csDropDown;
  ComboBox1.OnChange := ComboChange;

  ComboBox1.Items.Add('One');
  ComboBox1.Items.Add('Two');
  ComboBox1.Items.Add('Three');
end;

procedure TForm1.ComboChange(Sender: TObject);
begin
  { First Try }
  ComboBox1.DroppedDown := true;

  { Second Try }
  //PostMessage((Sender as TComboBox).Handle, CB_SHOWDROPDOWN, 1, 0);

  { Grasping at straws }
  //ShowCursor(true);
end;


end.
c0pp3rt0p
  • 141
  • 1
  • 8
  • Wouldn't it be enough to set the `AutoDropDown` property to `True`? – Andreas Rejbrand Mar 05 '19 at 21:51
  • Just tried that. The box doesn't drop when typing even though the docs say it should. – c0pp3rt0p Mar 05 '19 at 21:59
  • I think it does in a new, otherwise empty, VCL project, doesn't it? (It does in Delphi 10.2 on my Windows 7 system.) – Andreas Rejbrand Mar 05 '19 at 22:00
  • You are correct, autocomplete has to be `true`, but that breaks my filtering I'm working towards. While the cursor is still visible, I'm trying to get it working within the context of my sample. – c0pp3rt0p Mar 05 '19 at 22:05
  • I see. That appears to be necessary. – Andreas Rejbrand Mar 05 '19 at 22:08
  • I have noticed that using `OnKeyDown` instead of `OnChange` seems not to mess up the cursor quite as badly, but it isn't exactly the right event for this. – Andreas Rejbrand Mar 05 '19 at 22:10
  • 2
    I asked Google and found this: https://stackoverflow.com/a/33173679/282848 It works for me. (Still, I trust you are adding all code necessary to make your approach work. For instance, clicking an element in the drop down list will, by default, change the text and trigger the `OnChange` event again.) – Andreas Rejbrand Mar 05 '19 at 22:15
  • Cheers to @Andreas and @Sertac once again. `SendMessage((Sender as TComboBox).Handle, WM_SETCURSOR, 0, 0);` worked like a charm and yes this turned out to be a duplicate. Argh! If only there was a way to track the number of beers I owe you for all the other times your answers saved me. – c0pp3rt0p Mar 06 '19 at 04:09

0 Answers0