c# - Hitting Enter in Combobox clears text -
i have combo box list of items. when user starts typing drop down should open. hitting key should check text of combo box.
to accomplish set droppeddown = true inside keydown event. however, after did hitting key causes text of combo box empty.
private void cmbitems_keydown(object sender, keyeventargs e) { if (!e.keycode.equals(keys.enter)) { if (!cmbitems.droppeddown) { cmbitems.droppeddown = true; } } else { //check text } }
why combo box text cleared , there way around it?
my users prefer not use autocompletemode.suggestappend instead, if have to.
full sample code:
public class demo : form { private system.componentmodel.icontainer components = null; protected override void dispose(bool disposing) { if (disposing && (components != null)) { components.dispose(); } base.dispose(disposing); } private void initializecomponent() { this.lbltext = new system.windows.forms.label(); this.cmbitems = new system.windows.forms.combobox(); this.suspendlayout(); // // lbltext // this.lbltext.autosize = true; this.lbltext.location = new system.drawing.point(152, 47); this.lbltext.name = "lbltext"; this.lbltext.size = new system.drawing.size(155, 13); this.lbltext.tabindex = 17; this.lbltext.text = "text"; // // cmbitems // this.cmbitems.autocompletemode = system.windows.forms.autocompletemode.append; this.cmbitems.autocompletesource = system.windows.forms.autocompletesource.listitems; this.cmbitems.location = new system.drawing.point(12, 44); this.cmbitems.name = "cmbitems"; this.cmbitems.size = new system.drawing.size(121, 21); this.cmbitems.tabindex = 0; this.cmbitems.keydown += new system.windows.forms.keyeventhandler(this.cmbitems_keydown); // // demo // this.autoscaledimensions = new system.drawing.sizef(6f, 13f); this.autoscalemode = system.windows.forms.autoscalemode.font; this.clientsize = new system.drawing.size(338, 78); this.controls.add(this.lbltext); this.controls.add(this.cmbitems); this.name = "demo"; this.text = "demo"; this.resumelayout(false); this.performlayout(); } #endregion private system.windows.forms.label lbltext; private combobox cmbitems; datatable _items = new datatable(); public demo() { initializecomponent(); buildlistofitems(); } private void cmbitems_keydown(object sender, keyeventargs e) { if (!e.keycode.equals(keys.enter)) { if (!cmbitems.droppeddown) { cmbitems.droppeddown = true; } } else { //check text } lbltext.text = cmbitems.text; } //create demo list of items. private void buildlistofitems() { _items.columns.add("name").datatype = typeof(string); _items.columns.add("id").datatype = typeof(int); (int = (int)'a'; < (int)'a' + 26; i++) { createitem(i, "", 0); } cmbitems.datasource = _items; cmbitems.valuemember = "id"; cmbitems.displaymember = "name"; } private void createitem(int symbol, string prev, int count) { string newprev = string.format("{0}{1}", prev, (char)symbol); _items.rows.add(newprev, _items.rows.count); if (count < 4) createitem(symbol + 1, newprev, ++count); } }
i did of on in college , went , checked code. i'm not 100% sure, looking for?
private void combocode_texttype(object sender, eventargs e) { int itemsindex = 0; foreach (string item in cmbgagecode.items) { if (item.indexof(cmbgagecode.text) == 0) { cmbgagecode.selectedindex = itemsindex; cmbgagecode.select(cmbgagecode.text.length - 1, 0); break; } itemsindex++; } }
Comments
Post a Comment