Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
i have a combobox whit colors name item
i want change background color based on selected item
also, i want add new color in runtime
C#
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(13))
            {
                comboBox1.Items.Add(comboBox1.Text);
            }
        }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BackColor =Color. ????
        }

please help me
Posted
Updated 5-Sep-13 18:10pm
v2
Comments
[no name] 5-Sep-13 19:36pm    
Help with what? What have you tried? Where is the code that demonstrates your problem? What errors do you get when you run your code?
sahar11 5-Sep-13 19:59pm    
I add color name in combo at run time
what code I write Instead Of the one in question???
Philippe Mori 5-Sep-13 19:38pm    
What have you done so far?

Usually a way to do this, is to have a class that represent each item in the combo. Then you overwrite <çode>ToString() function so that Windows knows what to display and when a selection is made, you cast SelectedItem to your class type and then get the actual color from the property.

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:
C#
// string for use in comboBox1_KeyPress
private string comboBoxText;

// instance of the KnownColor enum in System.Drawing
// for use in comboBox1_KeyPress
private KnownColor newColorForComboBox;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // use Color.FromName to get the right color
    panel1.BackColor = Color.FromName(comboBox1.SelectedItem.ToString());
}

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // detect return key entered in comboBox1 text-area
    if (e.KeyChar == '\r')
    {
        // get the entered text
        comboBoxText = comboBox1.Text;

        // does the entered text match any of the Colors
        // defined in the KnownColor enum in System.Drawing ?

        // note that TryParse requires the use of the 'out modifier for its
        // final parameter, which is a variable name
        if (KnownColor.TryParse(comboBoxText, true, out newColorForComboBox))
        {
            // yes, we have a valid color name

            // do comboBox1's List (Items) already contain the color ?
            if(comboBox1.Items.Contains(comboBoxText))
            {
                // color's already known: advise the user, and select that color
                MessageBox.Show("That Color is already contained in the List");
                comboBox1.SelectedItem = comboBoxText;
            }
            else
            {
                // it's a valid new color: add it, use the color
                // notify the user
                comboBox1.Items.Add(comboBox1.Text);
                panel1.BackColor = Color.FromName(newColorForComboBox.ToString());
                MessageBox.Show("New Color: " + comboBoxText + " added to the List");
            }
        }
        else
        {
            // invalid color name: advise the user, clear combobox1's text-area
            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.
 
Share this answer
 
v2
Do the following: in your combo box, store items of not a string type, but object of some structure which contain colors and shoes their name, something like:
C#
struct ColorItem {
    internal ColorItem(Color color, string Name) { Color = color, Name = name; }
    internal Color Color;
    internal string Name;
    public override ToString() { return Name; } 
}


As you cam see ToString() will return the name of the color you choose to use in the call to the constructor. The combo box can hold items of any type at all. The only problem is: what will be shown in the UI. The answer is: whatever it's item's ToString() method returns; that's why I've overridden System.Object.ToString().

Now, add instances of this struct to your combo box. On selection event, you should cast the selected item to this structure in your handler of the event SelectedIndexChanged:
C#
ComboBox senderComboBox = (ComboBox)sender;
this.BackColor = ((ColorItem)senderComboBox.SelectedItem).Color;

I hope you got the idea.

—SA
 
Share this answer
 
Try this:

this.backcolor = color.red;

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900