简体   繁体   中英

In Windows Form Application Make list of Drop Down Without Changing Background Color

To make a list of Drop Down from designer as shown in picture I select DropDownStyle as DropDown list doing this it become a list but its background color is changed I also change BackColor property to window but color is remain same as of list now I want to Change background color of Dropdown as it is before making list.

在此处输入图片说明

Change the FlatStyle property to "Flat" or "Popup". There, you can change the combobox's backcolor. However, the combobox must lose focus in order for you to see the color because when it's selected, it is blue (vary by Windows current theme) when focused indicated that you've selected the item that you've selected


考虑这张图片

You can do this by changing DrawMode properties. After changing the DrawMode properties to DrawMode.OwnerDrawFixed , add DrawItem event and change your BackColor in within this.

Code:

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)
    {

    }
}

Output:

在此处输入图片说明

See System.Windows.Forms.DrawMode for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM