title: On C# WinForm ListBox how to clear the selection of multiple items with a key pressed?
body: <p>I'm trying to clear the selection of all items in a WinFom ListBox when the escape key is pressed. </p>
  
  <p>I created a KeyPress event handler in order to catch the event. It works when only one item is selected, but when multiple items are selected the event never triggers, . Any idea of what is happening?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Here I attached my event handler:</p>
  
  <pre><code>private void EscapeKeyPressed(object sender, KeyPressEventArgs e)
      {
          try
          {
              if (e.KeyChar == (char)Keys.Escape)
              {
                  switch (sender.GetType().GetProperty("Name").GetValue(sender, null).ToString())
                  {
                      case "LineLB":
                          LineLB.ClearSelected();
                          break;
                      case "ApplicationLB":
                          ApplicationLB.ClearSelected();
                          break;
                      default:
                          break;
                  }
              }
          }
          catch(Exception ex) { MessageBox.Show(ex.Message); }
  </code></pre>
  
  <p>--20150526--</p>
  
  <p>Thanks all you guys for your comments and suggestions. It was a focus problem as some of you mentioned before. I add a line of code in the SelectedIndexChange event handler, and here the comparison.</p>
  
  <p>Before</p>
  
  <pre><code>private void LineLB_SelectedIndexChanged(object sender, EventArgs e)
      {
          try
          {
              if (LineLB.SelectedItems.Count &gt; 1)
                  ClearControlsFromPanel(PanelUser);
              else{
                     ...
  </code></pre>
  
  <p>After</p>
  
  <pre><code>    private void LineLB_SelectedIndexChanged(object sender, EventArgs e)
      {
          try
          {
              if (LineLB.SelectedItems.Count &gt; 1){
                  ClearControlsFromPanel(PanelLine);
                  LineLB.Focus();
              }
              else{
                     ...
  </code></pre>