How to show text in combobox when no item selected?

前端 未结 16 1270
太阳男子
太阳男子 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 07:12

    I can't see any native .NET way to do it but if you want to get your hands dirty with the underlying Win32 controls...

    You should be able to send it the CB_GETCOMBOBOXINFO message with a COMBOBOXINFO structure which will contain the internal edit control's handle. You can then send the edit control the EM_SETCUEBANNER message with a pointer to the string. (Note that this requires at least XP and visual styles to be enabled.

    0 讨论(0)
  • 2020-11-30 07:13

    If none of the previous solution are working for you, why not add some validation on combobox something like,

        var orginalindex = 0;
    
        private void comboBox1_SelectedItemChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                comboBox1.Text = "Select one of the answers";
                comboBox1.SelectedIndex = comboBox1.SelectedIndex;
            }
            else
            {
                orginalindex = comboBox1.SelectedIndex;
            }
        }
    
    0 讨论(0)
  • 2020-11-30 07:18

    Here's how I do it. It might not be the best method, and offers least control; however, it's simple and quick and I thought it might be a good idea to share it so that more options are available for others.

    <ComboBox SelectedIndex="0">
        <ComboBoxItem Visibility="Collapsed">Please select one...</ComboBoxItem>
        <ComboBoxItem>1</ComboBoxItem>
        <ComboBoxItem>2</ComboBoxItem>
        <ComboBoxItem>3</ComboBoxItem>
        <ComboBoxItem>4</ComboBoxItem>
    </ComboBox>
    

    The idea behind this is that the initial selection is index 0, which is collapsed, so it's not available under selection for the user once they select something else. The downside is that you have to remember that if you're checking for a selected index, remember that index 0 means there was no selection made.

    0 讨论(0)
  • 2020-11-30 07:19

    I could not get @Andrei Karcheuski 's approach to work but he inspired me to this approach: (I added the Localizable Property so the Hint can be translated through .resx files for each dialog you use it on)

     public partial class HintComboBox : ComboBox
    {
        string hint;
        Font greyFont;
    
        [Localizable(true)]
        public string Hint
        {
            get { return hint; }
            set { hint = value; Invalidate(); }
        }
    
        public HintComboBox()
        {
            InitializeComponent();
        }
    
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
    
            if (string.IsNullOrEmpty(Text))
            {
                this.ForeColor = SystemColors.GrayText;
                Text = Hint;
            }
            else
            {
                this.ForeColor = Color.Black;
            }
        }
    
        private void HintComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if( string.IsNullOrEmpty(Text) )
            {
                this.ForeColor = SystemColors.GrayText;
                Text = Hint;
            }
            else
            {
                this.ForeColor = Color.Black;
            }
        }
    
    0 讨论(0)
提交回复
热议问题