0

I want to disable a combobox, but at the same time I want to let the users see the other options available (that is, I want to enable the dropdown).

By default, when ComboBox.Enabled = false, the dropdown is also disabled (nothing happens when we click on the combobox).

My first thought is to leave it enabled and handle the ComboBox.SelectedIndex event to set it back to the default value (I will just need to gray it out in some way.)

I am wondering if there is any native functionality like this that I am missing, or if there would be other way of doing it.

spajce
  • 7,044
  • 5
  • 29
  • 44

4 Answers4

4

Don't use a Combobox if you don't want the Combobox functionality. Use a ListView instead.

Anonymous
  • 575
  • 1
  • 4
  • 11
1

A "What You See Is What You Can't Get" Combobox seems a bad idea. I suggest using ListBox instead.

WAKU
  • 290
  • 3
  • 17
0

It's a hacky workaround, but it should accomplish something similar to your request:

public partial class Form1 : Form
{
    ComboBox _dummy;

    public Form1()
    {
        InitializeComponent();

        // set the style
        comboBox1.DropDownStyle = 
            System.Windows.Forms.ComboBoxStyle.DropDownList;
        // disable the combobox
        comboBox1.Enabled = false;

        // add the dummy combobox
        _dummy = new ComboBox();
        _dummy.Visible = false;
        _dummy.Enabled = true;
        _dummy.DropDownStyle = ComboBoxStyle.DropDownList;
        this.Controls.Add(_dummy);

        // add the event handler
        MouseMove += Form1_MouseMove;
    }

    void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        var child = this.GetChildAtPoint(e.Location);
        if (child == comboBox1)
        {
            if (!comboBox1.Enabled)
            {
                // copy the items
                _dummy.Items.Clear();
                object[] items = new object[comboBox1.Items.Count];
                comboBox1.Items.CopyTo(items, 0);
                _dummy.Items.AddRange(items);

                // set the size and position
                _dummy.Left = comboBox1.Left;
                _dummy.Top = comboBox1.Top;
                _dummy.Height = comboBox1.Height;
                _dummy.Width = comboBox1.Width;

                // switch visibility
                comboBox1.Visible = !(_dummy.Visible = true);
            }
        }
        else if (child != _dummy)
        {
            // switch visibility
            comboBox1.Visible = !(_dummy.Visible = false);
        }
    }
}
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
0

If using a ListBox as other answers suggested is not convenient. There is a way by creating a custom combobox and adding a ReadOnly property. Try this code :

class MyCombo : System.Windows.Forms.ComboBox
{
    public bool ReadOnly { get; set; }
    public int currentIndex;

    public MyCombo()
    {
        currentIndex = SelectedIndex ;
    }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        if (ReadOnly && Focused)
            SelectedIndex = currentIndex;

        currentIndex = SelectedIndex;

        base.OnSelectedIndexChanged(e);
    }

}

Usually the background color of read-only controls should not change, so I haven't done that part.

Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45