How to simulate combobox behaviour with a TEdit (Views: 23)


I'm trying to make a component that acts in certain cases like a combobox, i.e. when the user presses a button, a list box is shown and the user can select an item. The problem I have is that I need the list to hide itself whenever the user clicks the mouse outside the list (including clicks in non-windowed controls).


One way you can do this is by listening for CM_CANCELMODE messages in the parent of your drop-down list (presumably an edit control or something similar). You will probably then have to work out whether the message originated from a click on the drop-down list or elsewhere. In the code below, FPopup points to the list component:

procedure TMyPopupEdit.CMCancelMode(var Message: TCMCancelMode);
  P: TPoint;
  R: TRect;
  {Get the top-left coordinate of the Sender and see if it is within the popup
  control. If not, close the popup without changing the text in the edit box.}
  P.X := Message.Sender.Left;
  P.Y := Message.Sender.Top;
  R := Rect(0, 0, FPopup.Width, FPopup.Height);
  if not PtInRect(R, P) then
    if FPopupVisible then
      PopupCloseUp(FPopup, False);

<< Back to main page