c# - Selecting multiple ListBox items in the same ListBox with one click -
how can programmatically select other items in listbox clicking item in same listbox? c# winforms project.
for example when click clothes below, pants , shirts need highlight automatically. same goes auto parts highlight tires , transmissions.
clothes pants tires shirts transmissions auto parts
i have listbox bound datasource (itemlist) , tried add "itemindex" each item in list handle sorting (i'm sure there better way?) made sense me @ time, couldn't figure out how make work outside head...
here current code, please bare me still learning this. suggesstions fantastic.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace listbox_test { public partial class form1 : form { bindinglist<item> itemlist = new bindinglist<item>(); public form1() { initializecomponent(); showdata(); } private void showdata() { this.listbox1.datasource = itemlist; this.listbox1.displaymember = "itemname"; } private void form1_load(object sender, eventargs e) { additem(itemindex: 0, itemname: "clothes", itemprice: 0.95); additem(itemindex: 1, itemname: "pants", itemprice: 0.95); additem(itemindex: 2, itemname: "tires", itemprice: 0.95); additem(itemindex: 3, itemname: "shirts", itemprice: 0.95); additem(itemindex: 4, itemname: "transmissions", itemprice: 0.95); additem(itemindex: 5, itemname: "auto parts", itemprice: 0.95); } // add item list private void additem(int itemindex, string itemname, double itemprice) { itemlist.add(new item(itemindex, itemname, itemprice)); } private void listbox1_selectedindexchanged(object sender, eventargs e) { // selectchild(); ?? } } public class item { public int itemindex { get; set; } public string itemname { get; set; } public double itemprice { get; set; } public item(int itemindex, string itemname, double itemprice) { itemindex = itemindex; itemname = itemname; itemprice = itemprice; } } }
you need have sort of relationship (likely 1 key multiple values) declared between them such program knows item(s) is(are) related item(s)
as example, below how implemented string
string[]
relationship using dictionary<string, string[]>
:
dictionary<string, string[]> dict = new dictionary<string, string[]>(){ {"clothes", new string[] {"pants","shirts"}}, {"auto parts", new string[] {"tires","transmissions"}} };
then, putting listbox.selectionmode
multisimple
, , based on key
(string
), select value
(string[]
) using listbox.setselected
to implement want fully, however, found out quite tricky - if use selectedindex
event. there @ least 2 other things need consider:
- when select inside
selectedindexchanged
event handelr, cause program trigger anotherselectedindexchanged
. this, if not handled, may causestackoverflow
exception due recursion calls. - you might want have default functionality item not in
key
list (suchshirts
). so, might need record last selected item is. but, unfortunately,selecteditems
,selectedindices
oflistbox
not go selection chronological order, sequential order. thus, cannot infer last selected item eitherselecteditems
orselectedindices
, may need implement own "memory" last (singular) selected item.
considering few things above, final, safe, implementation may (commented):
//creates relationship dictionary<string, string[]> dict = new dictionary<string, string[]>(){ {"clothes", new string[] {"pants","shirts"}}, {"auto parts", new string[] {"tires","transmissions"}} }; private bool isprocessingselection = false; //prevents stack overflow list<string> listbox1_lastselections = new list<string>(); //creates memory private void listbox1_selectedindexchanged(object sender, eventargs e) { if (isprocessingselection) //to prevent stack overflow exception because of recursive call return; isprocessingselection = true; //set true whenever processing list<string> currentselections = listbox1.selecteditems.cast<string>().tolist(); string lastselection = listbox1_lastselections.count > currentselections.count ? listbox1_lastselections.except(currentselections).firstordefault() : currentselections.except(listbox1_lastselections).firstordefault(); //get last selected item comparison of current , last selection int index = listbox1.items.indexof(lastselection); //the last selected index (int = 0; < listbox1.items.count; ++i) { if (i == index) //do not process last selected index continue; listbox1.setselected(i, false); //make else false } if (dict.containskey(lastselection)) { //if last selection among item in dictionary, highlight rests string[] related = dict[lastselection]; (int = 0; < listbox1.items.count; ++i) if (related.contains(listbox1.items[i].tostring())) listbox1.setselected(i, true); } listbox1_lastselections = listbox1.selecteditems.cast<string>().tolist(); //update last selection isprocessingselection = false; //prepare next, non recursive call }
Comments
Post a Comment