///<summary>Sets the selected item(s) that match the func passed in. Will only work if the Items in the ListBox are ODBoxItems.</summary> ///<param name="fSelectItem">A func that takes an object that is the same type as the ODBoxItems Tags and returns a bool, i.e. ///x => x.ClinicNum==0.</param> public static void SetSelectedItem <T>(this ListBox listBox, Func <T, bool> fSelectItem) { for (int i = 0; i < listBox.Items.Count; i++) { ODBoxItem <T> odBoxItem = listBox.Items[i] as ODBoxItem <T>; if (odBoxItem == null) { continue; } if (fSelectItem(odBoxItem.Tag)) { listBox.SetSelected(i, true); } } }
///<summary>Sets the selected item that matches the func passed in. Will only work if the Items in the combo box are ODBoxItems.</summary> ///<param name="fSelectItem">A func that takes an object that is the same type as the ODBoxItems Tags and returns a bool, i.e. ///x => x.ClinicNum==0.</param> ///<param name="overrideText">The text to display in the combo box if a matching item cannot be found.</param> public static void SetSelectedItem <T>(this ComboBox combo, Func <T, bool> fSelectItem, string overrideText) { int idx = -1; for (int i = 0; i < combo.Items.Count; i++) { ODBoxItem <T> odBoxItem = combo.Items[i] as ODBoxItem <T>; if (odBoxItem == null) { continue; } if (fSelectItem(odBoxItem.Tag)) { idx = i; break; } } combo.SelectIndex(idx, overrideText); }