alondono
alondono

Reputation: 2701

How to fire the OnClick event of a TComboBox when the edit control of the combo box is clicked?

I have a TComboBox with Style := csDropDown. I want to make it so that if the user clicks on the edit box part of the combo box, then the whole text in that edit box is selected. So I thought about implementing the OnClick event of the combo box, but the OnClick event is fired only when the combo box list is being displayed. It doesn't work when the list is closed and only one item is visible, which is where I want to use it.

I have tried other events besides OnClick, like OnEnter, but all of the ones I have tried seem to work only when the combo box list is expanded, or when you click the little arrow on the right side of the component.

I have also tried with mouse events, like OnMouseDown, which are not published for combo boxes, but after managing to implement them, they also only work when the mouse is clicked on the little arrow to expand the list, or the expanded list, not on the edit box part of the combo box.

Upvotes: 0

Views: 1320

Answers (1)

Dave Nottage
Dave Nottage

Reputation: 3602

This worked for me:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TComboBox = class(Vcl.StdCtrls.TComboBox)
  protected
    procedure EditWndProc(var Message: TMessage); override;
  end;

  TForm1 = class(TForm)
    ComboBox1: TComboBox;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


{ TComboBox }

procedure TComboBox.EditWndProc(var Message: TMessage);
begin
  inherited;
  if Message.Msg = WM_LBUTTONDOWN then
    SelectAll;
end;

end.

Upvotes: 2

Related Questions