Question

I have a ToolStripComboBox which is bound to a List<string>. I'd like to set the visible text after initialization to String.Empty.

The problem is, that whatever I do, the text after initializing the control is always the first entry of my List (what is expected but I don't manage to clear this preselected text).

This is my relevant code:

    public frmPricelist(Pricelist pricelist)
    {
        _pricelist = pricelist;

        InitializeComponent();
        Init();
    }

    private void Init()
    {
        cmbHersteller.Items.Clear();
        cmbHersteller.ComboBox.DataSource = _pricelist.GetHersteller();

        Application.DoEvents(); // Inserted for testing purposes

        cmbHersteller.ComboBox.SelectedText = String.Empty; // does not change the value
        cmbHersteller.ComboBox.Text         = String.Empty; // does not change the value            
    }

Perhaps I miss the forest for the trees but I simply don't get it to work :).

Was it helpful?

Solution

In my opinion, the best approach is to actually add an empty item. Consider the following:

private void Init()
{
    cmbHersteller.Items.Clear();

    var list = _pricelist.GetHersteller();
    list.Insert(0, "");
    cmbHersteller.ComboBox.DataSource = list;

    cmbHersteller.ComboBox.SelectedIndex = 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top