title: How to bring a selected application window from a listbox to front
body: <p>I have a windows form application. In which in a listbox, I am showing title of all the opened windows. I have done up to this. Now I want to double click on a title in the listbox.. and this will activate that window and will show upfront. My code is like this::</p>
  
  <pre><code>    //this function is loading all the opened windows in the listbox 
  
      public void LoadOpenedWindows()
      {
          Process[] processlist = Process.GetProcesses();
  
          foreach (Process process in processlist)
          {
              if (!String.IsNullOrEmpty(process.MainWindowTitle))
              {
                  listBox1.Items.Add(process.MainWindowTitle);
  
             }
          }
          listBox1.DoubleClick += new EventHandler(ListBox1_DoubleClick);
  
      }
  </code></pre>
  
  <p>I tried to open the selected item by the following way.. But that is not working..</p>
  
  <pre><code>    private void ListBox1_DoubleClick(object sender, EventArgs e)
      {
          if (listBox1.SelectedItem != null)
          {
              //MessageBox.Show(listBox1.SelectedItem.ToString());
              const uint SW_SHOW = 5;
              const int SW_RESTORE = 5;
              string selected = listBox1.SelectedItem.ToString();
              IntPtr handleOfSelected = getHandle(selected);
              ShowWindowAsync(handleOfSelected, SW_RESTORE);
              SetForegroundWindow(handleOfSelected);
              //BringWindowToTop(handleOfSelected);
              //ShowWindow(handleOfSelected, SW_SHOW);
          }
      }
  
      public IntPtr getHandle(string selectedItem)
      {
          IntPtr hWnd = IntPtr.Zero;
          foreach (Process pList in Process.GetProcesses())
          {
              if (pList.MainWindowTitle.Contains(selectedItem))
              {
                  hWnd = pList.MainWindowHandle;
              }
          }
          return hWnd;
      }
  </code></pre>
  
  <p>If anyone have any idea or piece of code.. Please try to help.</p>