title: Setting ComboBox.DroppedDown = false resets selected item
body: <p>I have a custom ComboBox that inherits form Forms.ComboBox class. Its DataSource is bound.
  Its properties are initialized as follows:</p>
  
  <pre><code>this.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
  this.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
  this.DropDownStyle = ComboBoxStyle.DropDown;
  </code></pre>
  
  <p>OnDropDown its AutoCompleteMode is set to None, and then, in OnDropDownClosed, it is set to Suggest again (to avoid the problem desctibed here <a href="https://stackoverflow.com/questions/3064780/winforms-combobox-dropdown-and-autocomplete-window-both-appear">WinForms ComboBox DropDown and Autocomplete window both appear</a> )</p>
  
  <p>I have a logic implemented that validates the ComboBox and selects first matching item when some text is typed.</p>
  
  <p>When DropDown is shown and user types something and presses Enter, the first matching item is selected. Then I want to close DropDown. I do it by setting ComboBox.DroppedDown property to false. The problem is that when DropDown closes this way, the selected item resets to previously selected item. OnSelectedIndexChanged, OnSelectionChangeCommitted and OnSelectedValueChanged aren't called.</p>
  
  <p>I deal with it by remembering the index selected in OnSelectedIndexChanged and setting it again in OnDropDownClosed (on conditon that DropDown was closed manually). </p>
  
  <p><strong>Question:</strong>
  Why does the selected item, index, text and value reset when DroppedDown propery of ComboBox is set to false? Is there another way to close ComboBox manually that doesn't have this side effect?</p>
  
  <p><strong>EDIT</strong>: code to ilustrate the issue:</p>
  
  <pre><code>List&lt;string&gt; list = new List&lt;string&gt;();
              list.Add("item0");
              list.Add("item1");
              list.Add("item2");
              list.Add("item3");
              list.Add("item4");
  
              customComboBox1.DataSource = list;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public class CustomComboBox : ComboBox
      {
  
          public CustomComboBox(){ }
  
          #region OnKeyDown
          protected override void OnKeyDown(KeyEventArgs e)
          {
              if (!this.Enabled) e.Handled = true;
              else
              {
                  base.OnKeyDown(e);
                  if (e.KeyCode == Keys.Enter)
                  {
                      this.SelectedIndex = 2; // index changes here
  
                      Console.WriteLine("ENTER - before DroppedDown=false " +
                          this.SelectedIndex + " " + this.Text);
  
                      if (this.DroppedDown)
                      { 
                          // here index = 2, text = Item2
                          this.DroppedDown = false; 
                          // here index = previous index, text is still Item2
                          // different thing is displayed than is actually chosen
                          // OnSelectedIndexChanged wasn't raised
                      }
  
                      Console.WriteLine("ENTER - after DroppedDown=false " +
                          this.SelectedIndex + " " + this.Text);
  
                      e.Handled = true;
                  }
              }
          }
          #endregion
  
          #region OnSelectedIndexChanged
          protected override void OnSelectedIndexChanged(EventArgs e)
          {
               Console.WriteLine("OnSelectedIndexChanged " +
                   this.SelectedIndex + " " + this.Text);
  
                  base.OnSelectedIndexChanged(e);
          }
          #endregion
  
          #region OnDropDownClosed
  
          protected override void OnDropDownClosed(EventArgs e)
          {
              Console.WriteLine("OnDropDownClosed " +
                   this.SelectedIndex + " " + this.Text);
  
              base.OnDropDownClosed(e);
  
  
          }
  
          #endregion
  
      }
  </code></pre>
  
  <p>Console output looks like this (chosen item: 0, try to change to 2 while pressing Enter, after closing DropDown index is still 0):</p>
  
  <blockquote>
    <p>OnSelectedIndexChanged 0 item0</p>
    
    <p>OnSelectedIndexChanged 2 item2 </p>
    
    <p>ENTER - before DroppedDown=false 2 item2 </p>
    
    <p>OnDropDownClosed 0 item2 </p>
    
    <p>ENTER - after DroppedDown=false 0 item2</p>
  </blockquote>