How to show text in combobox when no item selected?

前端 未结 16 1360
太阳男子
太阳男子 2020-11-30 06:39

C# & .Net 2.0 question (WinForms)

I have set of items in ComboBox and non of them selected. I would like to show a string on combo \"Pl

16条回答
  •  星月不相逢
    2020-11-30 06:58

    Credit must be given to IronRazerz in a response to TextBox watermark (CueBanner) which goes away when user types in single line TextBox (not for RichTextBox).

    You will need to declare the following in your class:

    private const int CB_SETCUEBANNER = 0x1703;
    
    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]string lParam);
    

    Then you can use it with something like:

    SendMessage(this.comboBox1.Handle, CB_SETCUEBANNER, 0, "Please select an item...");
    

    This is assuming the Combo Box's DropDownStyle is set to DropDownList, as is the original poster's question.

    This should result in something like the following:

提交回复
热议问题