Search code examples
c#winformseventscomboboxtextchanged

What Event for ComboBox to change text color while typing


I'm programming in WinForms.
I have a ComboBox set with an initial gray ForeColor. My goal is to change the text color of this ComboBox when the user start to type something.

I tried to use _TextChanged and TextUpdate Events but don't work.

private void ComboBox1_TextChanged(Object sender, EventArgs e)
{
    ComboBox1.ForeColor = SystemColors.ControlText;
}

I already used the Event _SelectedIndexChanged to change the text color when the user select an item from the drop-down list, and it works well, but the text remains gray if the user types something (there is an AutoCompleteCustomSource collection associated to the ComboBox so the user can write instead to use the drop-down list).

Any suggestion?

EDIT
I have solved this way:

Registering in Form1.Designer.cs:

this.ComboBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.ComboBox1_KeyUp);` 

Using this code:

private void ComboBox1_KeyUp(Object sender, KeyEventArgs e) 
{ 
    ComboBox1.ForeColor = SystemColors.ControlText; 
}

Solution

  • Use KeyUp, KeyDown, or KeyPress events fired by the textbox. You probably don't want KeyPress for this purpose. TextChanged fires when the text has already been changed, which is why the new character being typed doesn't have a different color when you subscribe to it.