Yes, this is not standard behavior. I think you need to handle many events to implement your logic. I wrote the following example for you.
First, the combobox.DropDownStyle is set to DropDownList, this only allows user to select the items. When user select �lt;type new item>� change the DropDownStyle to dropdown, this allows user to type into the combobox. When leaving the combobox, add the new item to the combobox.item collection and set the DropDownStyle back to DropDownList. If combobox is databound, you need to add the item to the datasource. By the way, you need also consider after user typed they may click the dropdown button.
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.SelectedValueChanged += new EventHandler(comboBox1_SelectedValueChanged);
comboBox1.Validated += new EventHandler(comboBox1_Validated);
comboBox1.DropDown += new EventHandler(comboBox1_DropDown);
}
void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "<type items>")
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
}
}
void comboBox1_DropDown(object sender, EventArgs e)
{
checkItems();
}
void comboBox1_Validated(object sender, EventArgs e)
{
checkItems();
}
public void checkItems()
{
if (comboBox1.DropDownStyle == ComboBoxStyle.DropDown)
{
if (comboBox1.Text != "")
{
if (!comboBox1.Items.Contains(comboBox1.Text))
{
comboBox1.Items.Add(comboBox1.Text);
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
}
else
{
string temp = comboBox1.Text;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.SelectedItem = temp;
}
}
else
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
}
}
}
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.