How to change ForeColor from the ComboBox component you have selected?

Is it possible to change the appearance for the selected (not in the drop-down!) Element?

combobox.ForeColor changes the color of the text only for all elements in the drop-down list.

Edit: Options beelow ours

 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();
    }
+5
source share
3 answers

FlatStyle Popup Flat, . , , , . Windows 3D-; Flat Popup Web Windows Mobile, .

, , , , combobox, , Windows Vista . , , combobox DropDownList , Windows, . " ". DropDownList DropDown:

      Comparing the DropDownList style to the DropDown style under Windows Vista or later

, Windows, Vista . - DropDownStyle DropDown ( ).

FlatStyle System, , Windows. Standard , . .

( ):

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;
}

:

    ComboBox showing selected text in red

+9

FlatStyle Popup Flat, ForeColor.

Screen shot

, - ForeColor , DropDown .

:

 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;
    }
+4

You can use Cody Gray 'sugestion and add it to the same DropDownList style behavior:

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

Therefore, the user cannot edit the drop-down lists.

+1
source

All Articles