2

I using a System.Windows.Forms.ComboBox and I'm getting some weird unexpected behavior. In c#, I am dynically adding a few comboBoxes to my form and binding them to a list. The only fields I'm setting are DataSource,ValueMember, and DisplayMember. For some reason, after I bind to the list, the first item is selected. I cannot figure out what's going on.

My code looks like this:

 Control c = new System.Windows.Forms.ComboBox();

Looping through all my controls,

if (c?.GetType() == typeof (ComboBox))
{
    BindComboBox((ComboBox) c);
}


private void BindComboBox(ComboBox sender)
{
    DataTable table = DataGateway.GetTables(1);
    sender.DataSource = table;
    sender.ValueMember = "ID";
    sender.DisplayMember = "Name";

    //sender.SelectedIndex = -1; I tried with this and without this
}   

I also tried a second method but the same thing is happening -

private void BindComboBox(ComboBox sender)
{

    List<string> hiStrings = new List<string>() {"hi", "hello", "whats up"};
    sender.DataSource = hiStrings;

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mike Turner
  • 471
  • 1
  • 7
  • 22
  • I cannot duplicate this behavior when I set `sender.SelectedIndex = -1` I get a blank combobox with the 3 values in the dropdown. Are you doing something later, after the loop. – Charles May Feb 17 '16 at 16:17
  • I just set control Name and I add this control to a devex layoutcontrolItem and then to a devex LayoutControlGroup and then to a devex LayoutControl – Mike Turner Feb 17 '16 at 16:20
  • sorry, not sure I can offer anything else. I tried adding controls to a flowLayoutPanel and then looping through the controls to the BindCombobox method. and even tried creating the control and binding it before adding to the flp. Both placed blank comboboxes with items in the items collection without a hitch. This is using method2. I'll set up a DS and try it that way but I feel the results will be the same. SelectedIndex-1 should work. – Charles May Feb 17 '16 at 16:31

1 Answers1

1

First value is selected but is default behavior when You don't nothing change in ComboBox class settings

Modify the second method:

private void BindComboBox(ComboBox sender)
{

    List<string> hiStrings = new List<string>() {"hi", "hello", "whats up"};
    sender.DataSource = hiStrings;

    sender.SelectedItem = null;
}

And this give You empty ComboBox on Form

This solution is working and I tested.

Some helps links:

How to deselect the text of a combobox

Test method:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_MouseLeave(object sender, EventArgs e)
        {
          var comboBox = sender as ComboBox;

          this.TestMethod(comboBox);
        }

        private void TestMethod(ComboBox d)
        {
            var list = new List<string>() {"hi", "hello", "whats up"};
            d.DataSource = list;
            d.SelectedItem = null;
        }
    }
Community
  • 1
  • 1
blogprogramisty.net
  • 1,714
  • 1
  • 18
  • 22