Windows Develop Bookmark and Share   
 index > Windows Forms General > Typing in Combo Boxes
 

Typing in Combo Boxes

Hi,

I have a combo box with several options to chose from, one of which is a new option, so that the user can type in a new name. If I click the new option and then try to type a word, after each letter I type, the combo box appears as it does when you have clicked off of it. Then you can click it again and type one more letter, and the same thing happens. You should just be able to type the whole word and then click off the box. Has this happened to anyone else before? Or does anyone have any suggestions?

Katlyn
  • Moved byeryangMSFTMonday, September 07, 2009 9:07 AM (From:.NET Base Class Library)
  •  
KatlynD  Thursday, September 03, 2009 2:19 PM

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.
Ling Wang  Thursday, September 10, 2009 10:00 AM
Hello,
That is not standard behavior. Do you have any code that controls which form control currently has focus? Moves the mouse?

You should post a code sample of what you are doing?
Help people to help you. Don't force people to guess at what your code says.


Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Thursday, September 03, 2009 3:22 PM

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.
Ling Wang  Thursday, September 10, 2009 10:00 AM

You can use google to search for other answers

Custom Search

More Threads

• Disable show pictures under WebBrowser Control
• TreeView and the SystemTray
• Treeview node problem
• MaskedTextBox and Currency
• Combo box font color
• Double Buffering in Split Container Panels
• Customizing Datagridview column
• Datagrid - disabling different fields in different rows
• Child Form
• When pressing the <Enter> Key the last dialogue appears again!