Set hint or watermark or default text for ComboBox without adding it as item

前端 未结 3 1630
离开以前
离开以前 2020-12-04 03:46

So, I\'m trying to put some text showing up on the ComboBox before it has a selection made by the user.

That text should not be an item, selectable by t

相关标签:
3条回答
  • 2020-12-04 03:54

    Ahh, dropdownlist. That ignores the .Text property by only allowing it to be set from an item in the items list.

    However. You can manipulate that list I guess by handling the addition and removal of an item so that it's not seen in the dropdown

        private void Form1_Load(object sender, EventArgs e)
        {
            //add an item to the combobox at the top
                comboBox1.Items.Insert(0, "Please select an item");
            //set the text
                comboBox1.Text = "Please select an item";
        }
    
        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            //when we click to open the dropdown, we remove that item
            if (comboBox1.Items.Contains("Please select an item"))
                comboBox1.Items.RemoveAt(0);
        }
    
        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            //when we close the dropdown, if we select an item the dropdown
            //displays that item, if now we set back to our text.
            if (comboBox1.SelectedIndex == -1)
            {
                comboBox1.Items.Insert(0, "Please select an item");
                comboBox1.Text = "Please select an item";
    
            }
    
        }
    

    and depending on if your code would reset the form without a form_load being called, you would probably need something in the SelectedIndexchanged event to cover if you somewhere set the combobox's selectedindex back to -1 as this code wouldn't cover that.

    0 讨论(0)
  • 2020-12-04 04:04

    Hint, Cue Banner, Watermark or Default Text for ComboBox

    You can set hint for ComboBox without adding to items. To do so, if the ComboBox has DropDown or Simple mode, you can send a CB_SETCUEBANNER message to its inner edit control to set hint. If the combo has DropDownListMode, you can handle WM_PAINT or set it to owner draw to draw hint:

    ComboBox Select Text without adding Item ComboBox Select Text without adding Item

    Download

    You can clone or download the working example:

    • Download zip
    • GitHub repository

    Code

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    public class MyComboBox : ComboBox
    {
        public MyComboBox()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
        }
        const int CB_SETCUEBANNER = 0x1703;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);
        string hint;
        public string Hint
        {
            get { return hint; }
            set { hint = value; UpdateHint(); }
        }
        private void UpdateHint()
        {
            if (!this.IsHandleCreated)
                return;
            if (DropDownStyle == ComboBoxStyle.DropDownList)
                this.Invalidate();
            else
                SendMessage(Handle, CB_SETCUEBANNER, 0, Hint);
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            if (!string.IsNullOrEmpty(Hint))
                UpdateHint();
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            string text = Hint;
            base.OnDrawItem(e);
            if (e.Index > -1)
                text = this.GetItemText(this.Items[e.Index]);
            e.DrawBackground();
            TextRenderer.DrawText(e.Graphics, text, Font,
                e.Bounds, e.ForeColor, TextFormatFlags.TextBoxControl);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 04:15

    You can simply set a String to the Text property of that Combobox. This will give you the desired behaviour. Keep in mind that your code must take into account that this combo may have not any item selected. You can check this by the SelectedIndex property that will be -1.

    0 讨论(0)
提交回复
热议问题