tags:

views:

11779

answers:

3

I want to use a ComboBox with the DropDownList style (the one that makes it look like a button so you can't enter a value) to insert a value into a text box. I want the combobox to have a text label called 'Wildcards' and as I select a wildcard from the list the selected value is inserted in to a text box and the combobox text remains 'Wildcard'. My first problem is I can't seem to set a text value when the combobox is in DropDownList style. Using the properties pallet doesn't work the text value is simply cleared when you click off, adding comboBox.Text = "Wildcards"; to form_load doesn't work either. Can anyone help?

+4  A: 

The code you specify:

comboBox.Text = "Wildcards";

...should work. The only reason it would not is that the text you specify is not an item within the comboBox's item list. When using the DropDownList style, you can only set Text to values that actually appear in the list.

If it is the case that you are trying to set the text to Wildcards and that item does not appear in the list, and an alternative solution is not acceptable, you may have to be a bit dirty with the code and add an item temporarily that is removed when the drop-down list is expanded.

For example, if you have a form containing a combobox named "comboBox1" with some items and a button named "button1" you could do something like this:

private void button1_Click(object sender, EventArgs e)
{
    if (!comboBox1.Items.Contains("Wildcards"))
    {
        comboBox1.Items.Add("Wildcards");
    }

    comboBox1.Text = "Wildcards";
}

private void comboBox1_DropDown(object sender, EventArgs e)
{
    if (comboBox1.Items.Contains("Wildcards"))
        comboBox1.Items.Remove("Wildcards");
}

That's pretty quick and dirty but by capturing the DropDownClosed event too you could clean it up a bit, adding the "Wildcards" item back as needed.

BlackWasp
Thanks for that, I learnt a lot of my C# from the blackwasp web site and I still refer to it. Thanks!
You are most welcome :-)
BlackWasp
A: 

thanks for share

youtube
A: 

This method does look dirty and won't work. I guess.