开发者

How to change ForeColor of ComboBox's Selected Item?

开发者 https://www.devze.com 2023-02-17 11:10 出处:网络
Is it possible to change appearance for selected (not in drop down!) item开发者_开发百科? combobox.ForeColor is changing the text color only for all items into drop-down list.

Is it possible to change appearance for selected (not in drop down!) item开发者_开发百科?

combobox.ForeColor is changing the text color only for all items into drop-down list.

Edit: Variants are beelow, ours is

 public static void CBoxDrawItem(object sender, DrawItemEventArgs args)
    {
        var box = sender as ComboBox;
        if (box == null || args.Index < 0 || args.Index >= box.Items.Count)
            return;

        e.DrawBackground();
        var data = box.Tag as ControlData;
        var color = (args.State & DrawItemState.ComboBoxEdit) == 0 || data == null || !data.IsInDefaultState
            ? e.ForeColor : GetDefaultColor(e.ForeColor);
        using (var brush = new SolidBrush(color))
        {
            args.Graphics.DrawString(box.Items[args.Index].ToString(), args.Font, brush, args.Bounds.X, args.Bounds.Y);
        }
        args.DrawFocusRectangle();
    }


You don't have to change the FlatStyle to Popup or Flat to make this work. And you probably don't want to do that in the first place, because those styles tend to look really ugly when compared to the rest of your application's interface. Native Windows controls use a 3D-style appearance; the Flat and Popup styles are designed for Web or Windows Mobile applications, where they are a better fit.

I assume that you're asking this question because you have already written code to change the foreground color of the text displayed in the combobox, but have noticed that it isn't working under Windows Vista or later. That's because when the DropDownList style of combobox changed to look more like a button in those versions of Windows, it also lost support for custom text color. Instead, the selected text is always displayed in the standard "Window Text" color. Compare the DropDownList style to the regular DropDown style combobox:

     

How to change ForeColor of ComboBox's Selected Item?

Visually, the two comboboxes look the same in earlier versions of Windows, but not under Vista and later. The key to getting your custom foreground color to appear is changing the DropDownStyle property of your combobox control to DropDown (which is actually the default).

I also like to set the FlatStyle property to System so that you get all the nifty fade-in and fade-out effects offered by the native Windows controls. The Standard style attempts to emulate those effects in managed code, but it just doesn't have quite the right feel. I care about the little things.

Then you can use the following code (as originally suggested in Adrian's answer):

public Form1()
{
   InitializeComponent();

   // Set custom combobox styles
   comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
   comboBox1.FlatStyle = FlatStyle.System;

   // Attach relevant event handler methods
   comboBox1.DropDown += new EventHandler(comboBox1_DropDown);
   comboBox1.DropDownClosed += new EventHandler(comboBox1_DropDownClosed);
}

void comboBox1_DropDown(object sender, EventArgs e)
{
   // Optionally, revert the color back to the default
   // when the combobox is dropped-down
   //
   // (Note that we're using the ACTUAL default color here,
   //  rather than hard-coding black)
   comboBox1.ForeColor = SystemColors.WindowText;
}

void comboBox1_DropDownClosed(object sender, EventArgs e)
{
   // Change the color of the selected text in the combobox
   // to your custom color
   comboBox1.ForeColor = Color.Red;
}

To produce the following effect:

   

How to change ForeColor of ComboBox's Selected Item?


If you can change the FlatStyle of the combo box to Popup or Flat the color of the selected item will also change when you change the ForeColor.

How to change ForeColor of ComboBox's Selected Item?

To change only the color of the selected item you can implement some kind of workaround and change the ForeColor each time the DropDown is opened or closed.

Code Sample:

 public Form1()
    {
        InitializeComponent();

        comboBox1.FlatStyle = FlatStyle.Popup;

        comboBox1.DropDown += new EventHandler(comboBox1_DropDown);
        comboBox1.DropDownClosed += new EventHandler(comboBox1_DropDownClosed);
    }

    void comboBox1_DropDownClosed(object sender, EventArgs e)
    {
        comboBox1.ForeColor = Color.Red;
    }

    void comboBox1_DropDown(object sender, EventArgs e)
    {
        comboBox1.ForeColor = Color.Black;
    }


You can use the Cody Gray' sugestion and add it to has the same DropDownList Style behaviour:

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}

On this way the user can't edit the combobox.

0

精彩评论

暂无评论...
验证码 换一张
取 消