Search code examples
delphicomboboxedithintdelphi-10-seattle

TComboBox TextHint to Display While Focused


EM_SETCUEBANNER sets the textual cue, or tip, that is displayed by the TEdit control to prompt the user for information, but how do I set the textual cue of a TComboBox control and change it’s behaviour so that it only disappears when the user starts typing? Here is the code that I use to implement EM_SETCUEBANNER on a TEdit control:

SendMessage(TEdit.Handle, EM_SETCUEBANNER, wParam, Integer(PWideChar(HintText)));

How do I need to alter the above code to achieve the desired output with a TComboBox control?


Solution

  • There is an equivilent CB_SETCUEBANNER message.

    Sets the cue banner text that is displayed for the edit control of a combo box.

    SendMessage(TComboBox.Handle, CB_SETCUEBANNER, 0, LPARAM(PWideChar(HintText)));
    

    That being said, TEdit has a published TextHint property that internally uses EM_SETCUEBANNER, and TComboBox has a published TextHint property that internally uses EM_SETCUEBANNER on XP and CB_SETCUEBANNER on Vista+.

    I'm not sure when TEdit.TextHint was added, but TComboBox.TextHint was added in Delphi 2009:

    Delphi 2009 - TextHint in TComboBox


    Update: Note that the wParam value of CB_SETCUEBANNER must be 0. As such, it does not support the ability to make the banner text remain visible while an empty ComboBox has focus. If you need that, you will have to get the HWND of the ComboBox's edit field (see GetComboBoxInfo() and CB_GETCOMBOBOXINFO) and then send it EM_SETCUEBANNER directly (this is what the TextHint property does on XP) so you can set its wParam parameter to TRUE.

    uses
      ..., Winapi.CommCtrl;
    
    var
      info: TComboBoxInfo;
    begin
      info.cbSize := sizeof(info);
    
      GetComboBoxInfo(TComboBox.Handle, info);
      // or: SendMessage(TComboBox.Handle, CB_GETCOMBOBOXINFO, 0, LPARAM(@info)));
    
      SendMessage(info.hwndItem, EM_SETCUEBANNER, TRUE, LPARAM(PWideChar(HintText)));
    end;