// ---------------------------------------------------------------------------------------- /// <!-- Value --> /// <summary> /// Given a combo box and a value select the item in the drop list that matches if any /// </summary> /// <remarks> /// This gets complicated because SelectedValue can not be set or retrieved until after /// the ComboBox is displayed to the user /// </remarks> /// <param name="form">allows no-op if the control is not on the form</param> /// <param name="drop"></param> /// <param name="value"></param> public static void Value(Form form, ref ComboBox drop, object value) { if (Scrape.Active(form, drop)) { if (Is.Null(value)) { drop.Text = ""; } else { string str = TreatAs.StrValue(value, ""); int oldIndex = drop.SelectedIndex; drop.SelectedIndex = -1; string oldText = drop.Text; drop.Text = ""; // ---------------------------------------------------------------------90 // Try to select by value (this should work under optimal conditions) // ---------------------------------------------------------------------90 try { drop.SelectedValue = value; } catch { } if (drop.SelectedValue == null || drop.SelectedValue.ToString() == "") { try { drop.SelectedValue = (object)str; } catch { } } if (drop.SelectedValue == null || drop.SelectedValue.ToString() == "") { try { drop.SelectedValue = TreatAs.IntValue(value, -1); } catch { } } // ---------------------------------------------------------------------90 // Try to select item by value, by item and by a sequential search // ---------------------------------------------------------------------90 if (drop.SelectedIndex < 0) { SetByIndex(form, drop, drop.Items.IndexOf(value)); } if (drop.SelectedIndex < 0) { try { drop.SelectedItem = value; } catch { } } if (drop.SelectedIndex < 0) { try { drop.SelectedItem = (object)str; } catch { } } if (drop.SelectedIndex < 0) { SetByIndex(form, drop, Scrape.FindIndexOf(form, drop, value)); } // ---------------------------------------------------------------------90 // Give up and just set the text to the value // ---------------------------------------------------------------------90 if (drop.SelectedIndex < 0) { SetByIndex(form, drop, drop.FindStringExact(str)); } if (drop.SelectedIndex < 0 && str.Length > 6) { SetByIndex(form, drop, drop.FindString(str)); } if (drop.SelectedIndex < 0) { if (drop.SelectedText == null || drop.SelectedText == "") { try { drop.SelectedText = str; } catch { } } if (drop.Text == null || drop.Text == "") { drop.Text = str; } } } } }