Combobox DropDownList Style with white background

2019-07-27 11:48发布

I want to have an uneditable ComboBox but still show a white background colour, so it is effectively styled like the default ComboBox style (DropDown). The ComboBoxStyle.DropDownList only provides the standard "disabled" looking grey back colour. Simply setting BackColor = Color.White has no effect.

DropDownList: DropDownList

DropDown: DropDown

标签: c# combobox
4条回答
forever°为你锁心
2楼-- · 2019-07-27 11:56

To make a ComboBox DropDownList look like a ComboBox DropDown:

  1. Add a ComboBox to the WinForm. Go to Properties Explorer. Select DropDownStyle > DropDownList. Then select FlatStyle > Flat.
  2. Add a Panel to the WinForm. Go to Properties Explorer. Select BorderStyle > FixedSingle.
  3. Drag ComboBox onto Panel. With ComboBox active, go to Properties Explorer > Dock > Fill.
  4. With ComboBox active, hold the ‘Shift’ key, select the Panel to make it active as well (order of selection is important).
  5. Go to the Layout Toolbar (View > ToolBars > Layout) and select ‘Make Same Size’.
  6. When you run your program, the DropDownList ComboBox should look like a DropDown ComboBox
查看更多
看我几分像从前
3楼-- · 2019-07-27 11:58

Having struggled trying to get the control looking identical to the DropDown ComboBox style I had to settle with overriding the OnKeyPress event so that it restricted the user from been able to edit the control. As a side note I would also recommend overriding the appropriate event to prevent users pasting values into the ComboBox (how to disable copy, Paste and delete features on a textbox using C#).

protected override void OnKeyPress(KeyPressEventArgs e)
{
    e.Handled = true;
}
查看更多
贪生不怕死
4楼-- · 2019-07-27 12:09

You will have to create your own ComboBox with custom drawing or use a third-party control such as Infragistics UltraCombo

public class MyComboBox : ComboBox
    {
        public MyComboBox()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
          // Repaint here
        }
    }
查看更多
萌系小妹纸
5楼-- · 2019-07-27 12:18

I played around with this for a while and didn't want to do anything too involved. Those ideas above probably work but all I did was change the flatStyle property from "standard" to "flat".

Although not perfect, it at least changes the background that grey/disabled look to white.

You can see the comparison here:

Heating Source #1 > DropdownList > flat (the final decision since dropdown was allowing users to enter bad data)

Heater Source #2 > Dropdown > Standard (the default which looks nice)

Housing Type > Dropdown > Flat

Heating Source #1 Vendor > DropdownList > Standard (the default which looks disabled greyenter image description here)

查看更多
登录 后发表回答