在Windows窗体应用程序中生成下拉列表而不更改背景颜色

时间:2017-04-10 05:09:42

标签: c# winforms

要从设计器制作Drop Down列表,如图所示我选择DropDownStyle作为DropDown列表这样做它变成一个列表但是它的背景颜色被改变我也将BackColor属性改为窗口但是颜色与列表现在保持相同我想在制作列表之前更改Dropdown的背景颜色。

enter image description here

2 个答案:

答案 0 :(得分:0)

将FlatStyle属性更改为" Flat"或" Popup"。在那里,你可以改变组合框的背景色。但是,组合框必须失去焦点才能让您看到颜色,因为当它被选中时,它是蓝色的(因Windows当前主题而异),当聚焦表示您已选择了该项目时,#39已经选择了


Consider this image

答案 1 :(得分:0)

您可以通过更改DrawMode属性来执行此操作。将DrawMode属性更改为DrawMode.OwnerDrawFixed后,添加DrawItem个事件并在此内更改BackColor

代码:

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
    comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
}

private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
    try
    {
        // Draw the background of the ListBox control for each item.
        e.DrawBackground();
        // Define the default color of the brush as black.
        Brush myBrush = Brushes.Black;

        // Draw the current item text based on the current Font 
        // and the custom brush settings.
        e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
        // If the ListBox has focus, draw a focus rectangle around the selected item.
        e.DrawFocusRectangle();
    }
    catch (Exception ex)
    {

    }
}

输出:

enter image description here

有关详细信息,请参阅System.Windows.Forms.DrawMode