I kind of wish that no one had posted a solution here, since I feel the OP really needs to have shown us some evidence of their efforts so far to solve their problem. However, since solutions have been posted, and, imho, an effective "socratic pedagogic" mode of dialogue pre-empted, I'll "say on."
Here were my goals in taking Sahar11's code, and modifying it: when the user typed something in the text-area of the ComboBox, and pressed 'Return:
1. if the user entered a bunch of "nonsense" for a color name:
nothing should happen.
2. if the user entered a name of a color
already in the list of Items of the ComboBox: that color name should
not be added to the list (creating duplicates), but the existing color, of the same name,
should be selected.
3. if the user entered a name of a color which was valid, and which was not already in the ComboBox's Items list: that color
should be added to the ComboBox,
and selected.
4. in every case that a valid color is typed-in by the user: the Panel, 'panel1, should have its background-color set to that color.
Of course, the ComboBox must "do the right thing" when the user just selects an Item by direct action using whatever pointing, clicking device (mouse, touch) they have available, or by using standard short-cut keys (like F4 to cause a selected ComboBox to drop its list down, and up-down arrow-keys to navigate the Items' list !
The following code assumes:
1. a Windows Form with a ComboBox, 'comboBox1, and a Panel, 'panel1
2. the Windows Form has the System.Drawing namespace activated by a 'using statement.
3. that 'comboBox1's Items list has had some entries of valid color names added to it at design-time.
What do I mean by a "valid color name" ? I refer to any of the values of the System.Drawing.KnownColor enum: [
^].
Here's the (arbitrary) list of color names I typed in at design-time:
WhiteSmoke
GhostWhite
LightSlateGray
SlateGray
ControlDarkDark
Black
Red
FireBrick
Green
ForestGreen
Blue
Yellow
LightYellow
Ivory
Note that the list contains color names you usually see at design time in the color-picker from both the Web tab, like "GhostWhite," and the System tab, like "ControlDarkDark." Also note that no effort was made here to allow entry of colors in other typical formats, like in #hexadecimal, or in #red,#green,#blue values: that could be done, of course, by adding more elaborate parsing of the text in the ComboBox.
Now: to the code:
private string comboBoxText;
private KnownColor newColorForComboBox;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
panel1.BackColor = Color.FromName(comboBox1.SelectedItem.ToString());
}
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
comboBoxText = comboBox1.Text;
if (KnownColor.TryParse(comboBoxText, true, out newColorForComboBox))
{
if(comboBox1.Items.Contains(comboBoxText))
{
MessageBox.Show("That Color is already contained in the List");
comboBox1.SelectedItem = comboBoxText;
}
else
{
comboBox1.Items.Add(comboBox1.Text);
panel1.BackColor = Color.FromName(newColorForComboBox.ToString());
MessageBox.Show("New Color: " + comboBoxText + " added to the List");
}
}
else
{
MessageBox.Show("There is no valid Color with that Name");
comboBox1.Text = string.Empty;
}
}
}
Note: the calls to 'MessageBox.Show("some string"); in the code, above, are just for educational purposes.
Note also, that: in reality, the 'Items of a ComboBox are an instance of System.Windows.Forms.ComboBox.ObjectCollection: for brevity's sake I've used the term "list" here.
For better, and worse, a ComboBox's Item collection contains instances of type 'Object: because of the flexibility in the way color values can be assigned, and the way a ComboBox permits access by strings: in this code I "got away with" not having to deal with converting from Object to string, or from string to Object.