c# - How can I prevent auto-select in ComboBox on drop-down except for exact matches? -
i have combobox part of detail display related data grid containing rows database. no binding combobox exists, doing manually. combobox allows manual entry, if text field, while still providing drop-down of choices.
my issue if have manually entered text in field, , drop-down clicked, combobox apparently wants seek out match. also, appears search simple, kg
matches kg/day
. must avoid , force exact match.
but further, think need able govern entire process myself, because further complicate matter, drop-down item read kg/day - kilograms/day
. database field data fetched, however, storing portion prior hyphen, kg/day
.
so, need intercept drop-down action in way allows me 2 things:
1) perform own search find whether or not have ad-hoc text, or "real" match. in selected drop-down; in other words, have kg/day
, not kg
.
2) eliminate auto-search behavior combobox wants do.
i have tried getting in front of these things using method handlers in form, such
combobox::dropdown() , combobox::dropdownclosed(),
but seems these still don't allow me stop basic combobox searching/matching.
i have tried creating class of own inherited combobox, don't know override, or in general how go getting want, stopping don't.
so, that, thank advice.
edit: expand on tried already... in inherited class, attempting use wndproc
override. based on advice found in forum, goal intercept combobox message lb_findstring
, replace lb_findstringexact
. post suggested combobox defaulted lb_findstring
, fits see doing, , subbing lb_findstringexact
cure problem. trouble is, unless got bad definition lb_findstring
, never received.
here's enum:
[flags] public enum listboxflags { lb_addstring = 0x0180, lb_setsel = 0x0185, lb_getselitems = 0x0191, lb_getselcount = 0x0190, lb_getcursel = 0x0188, lb_selectstring = 0x018c, lb_setcursel = 0x0186, lb_findstring = 0x018f, lb_findstringexact = 0x01a2, lb_getcount = 0x018b, lb_getsel = 0x0187, lb_gettext = 0x0189, lb_resetcontent = 0x0184, lb_sethorizontalextent = 0x0194, lb_gethorizontalextent = 0x0193, lb_gettopindex = 0x018e, lb_settopindex = 0x0197, lb_insertstring = 0x0181, lb_deletestring = 0x0182, lb_getitemdata = 0x0199 }
made sample code might - can use guide.
the idea handle textchanged
event of combobox
, , modify combobox list items
@ point. example below modify list add current text (most important, not change text when click combobox) , other items meet search criteria.
i don't think need code re-initialize list items when focus lost, left in there in case.
//contains list of default items combobox items list<string> combolist = new list<string>(); public form1() { initializecomponent(); initcombolist(); //initialize defaults initcombobox(); //initialize combobox list items } //fills defaults combobox items private void initcombolist() { combolist.add("red"); combolist.add("blue"); combolist.add("green"); } //initializes combobox items private void initcombobox() { combobox1.items.clear(); foreach (string s in combolist) combobox1.items.add(s); } //occurs when text changes in combobox private void combobox1_textchanged(object sender, eventargs e) { string curtext = combobox1.text; insertintocombobox(curtext); //insert current text combobox combobox1.select(curtext.length, 0); //if don't this, cursor goes index 0 :-( } //called whenever desired insert current text combobox items private void insertintocombobox(string curtext) { combobox1.items.clear(); //only add current text if it's not in list of defaults , not empty string if (combolist.contains(curtext) == false && curtext.length > 0) combobox1.items.add(curtext); foreach (string s in combolist) combobox1.items.add(s); } //called whenever combobox loses focus private void combobox1_leave(object sender, eventargs e) { initcombobox(); }
Comments
Post a Comment