Search code examples
c#.netwinformscomboboxcontrols

Can I disable a combobox in WinForms without graying it out?


Is there a way to disable value change for a combo box without graying it out in windows forms? I saw a few posts but they were for WPF and didnt help my situation.


Solution

  • Setting these on your comobobox will do the trick you are looking for, Combo is enabled but nobody can change or type anything so Appearance = Enabled, Behaviour = Disabled :)

            comboBox1.DropDownHeight = 1;
            comboBox1.KeyDown += (s, e) => e.Handled = true;
            comboBox1.KeyPress += (s, e) => e.Handled = true;
            comboBox1.KeyUp += (s, e) => e.Handled = true;
    

    If for some reason you cannot use lambdas then following handlers can be associated. Right Click -> Paste has to be handled additionally if you have DropDownStyle = DropDown.

        //void comboBox1_KeyUp(object sender, KeyEventArgs e)
        //{
        //    e.Handled = true;
        //}
    
        //void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    e.Handled = true;
        //}
    
        //void comboBox1_KeyDown(object sender, KeyEventArgs e)
        //{
        //    e.Handled = true;
        //}