I have an editable ComboBox that should contain a path. The user can select several default paths (or enter his own) from a dropdown list, such as %ProgramData%\\Microsoft\\Windows\\Start Menu\\Programs\\ (All Users). The items in the dropdown list contain a short explanation, like the (All Users) part in the former example. Upon selection of such an item, I want to remove this explanation, so that a valid path is displayed in the ComboBox.
I currently strip the explanation out of the string and try to change the text via setting the Text property of the ComboBox. But this doesn’t work, the string is parsed correctly, but the displayed text won’t update (it stays the same as in the dropdown list, with the explanation).
private void combobox_TextChanged(object sender, EventArgs e) {
//..
string destPath = combobox.GetItemText(combobox.SelectedItem);
destPath = destPath.Replace("(All Users)", "");
destPath.Trim();
combobox.Text = destPath;
//..
}
I found the solution in a similar question, by using BeginInvoke()
Using Nikolay’s solution, my method now looks like this:
The workaround is required, since BeginInvoke requires the control to be loaded or shown, which isn’t necessarily the case if the program just started. Got that from here.