public void SetComboBoxItems(ComboBox control, string selectedText, object[] val) { if (control.InvokeRequired) { SetComboBoxItemsCallback d = SetComboBoxItems; try { Invoke(d, new object[] { control, selectedText, val }); } catch (Exception) { } } else { control.Items.Clear(); control.Items.AddRange(val); control.Text = selectedText; } }
/// <summary> /// Replaces the items of a combobox /// </summary> /// <param name="container">The control that contains the control to change</param> /// <param name="control">The control to change</param> /// <param name="selectedIndex">The new selection index</param> /// <param name="val">The new content</param> public static void SetComboBoxItems(Control container, ComboBox control, int selectedIndex, object[] val) { if (container.InvokeRequired) { SetComboBoxItemsCallback d = SetComboBoxItems; try { container.Invoke(d, new object[] { container, control, selectedIndex, val }); } catch (Exception) { } } else { control.Items.Clear(); control.Items.AddRange(val); if (control.Items.Count > 0) { control.SelectedIndex = selectedIndex; } } }