c# - Customize TextBox autocomplete -
how change autocomplete on textbox? want when type string, box suggest items containing string instead of starting with.
my code is:
class myclass { private autocompletestringcollection autocompletelist = new autocompletestringcollection(); public myclass() { initializecomponent(); autocompletelist.addrange(listnames.select(x=>x.name).toarray()); textboxname.autocompletecustomsource = autocompletelist; textboxname.autocompletesource = autocompletesource.customsource; textboxname.autocompletemode = autocompletemode.suggest; textboxname.keydown += textboxtextname_keydown; } private void textboxclient_keydown(object sender, keyeventargs e) { if (e.keydata == keys.enter) { this.name = (sender textbox).text; } } } what want:
looking @ code have need 1 line of code. line is:
this work if start of string entered
//suggestion textboxname.autocompletemode = autocompletemode.suggest; //suggest , autocomplete textboxname.autocompletemode = autocompletemode.suggestappend; this work contains method works custom control
you can make custom textbox control fits needs.
custom textbox class:
public class autocompletetextbox : textbox { private listbox _listbox; private bool _isadded; private string[] _values; private string _formervalue = string.empty; public autocompletetextbox() { initializecomponent(); resetlistbox(); } private void initializecomponent() { _listbox = new listbox(); this.keydown += this_keydown; this.keyup += this_keyup; } private void showlistbox() { if (!_isadded) { parent.controls.add(_listbox); _listbox.left = left; _listbox.top = top + height; _isadded = true; } _listbox.visible = true; _listbox.bringtofront(); } private void resetlistbox() { _listbox.visible = false; } private void this_keyup(object sender, keyeventargs e) { updatelistbox(); } private void this_keydown(object sender, keyeventargs e) { switch (e.keycode) { case keys.enter: case keys.tab: { if (_listbox.visible) { text = _listbox.selecteditem.tostring(); resetlistbox(); _formervalue = text; this.select(this.text.length, 0); e.handled = true; } break; } case keys.down: { if ((_listbox.visible) && (_listbox.selectedindex < _listbox.items.count - 1)) _listbox.selectedindex++; e.handled = true; break; } case keys.up: { if ((_listbox.visible) && (_listbox.selectedindex > 0)) _listbox.selectedindex--; e.handled = true; break; } } } protected override bool isinputkey(keys keydata) { switch (keydata) { case keys.tab: if (_listbox.visible) return true; else return false; default: return base.isinputkey(keydata); } } private void updatelistbox() { if (text == _formervalue) return; _formervalue = this.text; string word = this.text; if (_values != null && word.length > 0) { string[] matches = array.findall(_values, x => (x.tolower().contains(word.tolower()))); if (matches.length > 0) { showlistbox(); _listbox.beginupdate(); _listbox.items.clear(); array.foreach(matches, x => _listbox.items.add(x)); _listbox.selectedindex = 0; _listbox.height = 0; _listbox.width = 0; focus(); using (graphics graphics = _listbox.creategraphics()) { (int = 0; < _listbox.items.count; i++) { if (i < 20) _listbox.height += _listbox.getitemheight(i); // item width larger current 1 // set new max item width // getitemrectangle not work me // add little space using '_' int itemwidth = (int)graphics.measurestring(((string)_listbox.items[i]) + "_", _listbox.font).width; _listbox.width = (_listbox.width < itemwidth) ? itemwidth : this.width; ; } } _listbox.endupdate(); } else { resetlistbox(); } } else { resetlistbox(); } } public string[] values { { return _values; } set { _values = value; } } public list<string> selectedvalues { { string[] result = text.split(new[] { ' ' }, stringsplitoptions.removeemptyentries); return new list<string>(result); } } } usage:
string[] namearray = { "name1", "name2", "name3", "bla name" }; autocompletetextbox tb = new autocompletetextbox(); tb.values = namearray; tb.location = new point(10,10); tb.size = new size(25,75); this.controls.add( tb ); i got code custom control from: so question - autocomplete contains

Comments
Post a Comment