Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

We have a WinForms control that is an extended version of ComboBox that supports "cue banners" (aka watermarks) when there is no selection or text. Our control is similar to this implementation making use of CB_SETCUEBANNER.

However, when we set DropDownStyle for the control to ComboBoxStyle.DropDown (that is, also allows free text entry) the cue banner is showing, just not in italics (which is how it usually shows).

Does anyone know how to draw the cue banner in italics for a combo box in ComboBoxStyle.DropDown mode???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
216 views
Welcome To Ask or Share your Answers For Others

1 Answer

By design. When the Style = DropDown, the text portion of the combobox is a TextBox. Which displays the cue banner in non-italic style. You can verify with this code. It is otherwise important to make the distinction between the banner and the actual selection visible when the Style = DropDownList, no doubt the reason they chose to display it italic. TextBox does it differently, it hides the banner when it gets the focus.

Throwing in a non exhausting version:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class CueComboBox : ComboBox {
    private string mCue;
    public string Cue {
        get { return mCue; }
        set {
            mCue = value;
            updateCue();
        }
    }
    private void updateCue() {
        if (this.IsHandleCreated && mCue != null) {
            SendMessage(this.Handle, 0x1703, (IntPtr)0, mCue);
        }
    }
    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        updateCue();
    }
    // P/Invoke
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, string lp);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...