1

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 :).

user1567896
  • 2,398
  • 2
  • 26
  • 43

1 Answers1

1

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;
}
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • I have thought of this, too. But I'd like to avoid this if possible. – user1567896 Dec 20 '13 at 13:29
  • 1
    @user1567896, the problem is that a data bound combo box uses it's data bound items directly. There are some very nasty work arounds, that I'm not sure even work with the tool strip version of the combo box. This is the most accurate and appropriate way. – Mike Perrenoud Dec 20 '13 at 13:33
  • 1
    I'm afraid you are right. I've tried some things but this little control is indeed very nasty. I'll use a text-dummy like ** if no filtering is needed. Thanks for your answer! – user1567896 Dec 20 '13 at 13:47