Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a windows form application. In which in a listbox, I am showing title of all the opened windows. I have done upto 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::
C#
//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);
            
        }


I tried to open the selected item by the following way.. But that is not working..
C#
private void ListBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                //MessageBox.Show(listBox1.SelectedItem.ToString());
                const uint SW_SHOW = 5;
                string selected = listBox1.SelectedItem.ToString();
                IntPtr handleOfSelected = getHandle(selected);
                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;
        }


If anyone have any idea or piece of code.. Please try to help.
Posted
Updated 6-Mar-15 1:19am
v2
Comments
CHill60 6-Mar-15 7:51am    
That's working fine for me ... what do you mean by "not working"?
JaideepChandra 6-Mar-15 7:56am    
When I double click on any item in the listbox.. That window is not coming upfront.
Joan Magnet 6-Mar-15 9:11am    
Try with:

SetActiveWindow(hWnd);
SetFocus(hWnd)
JaideepChandra 6-Mar-15 23:35pm    
Sorry to reply late..It's not working. The selected window is not maximizing and is not coming upfront.

You need to call ShowWindowAsync.

Use the following signature:
C#
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);


Ad your double click hadler should look like this:

C#
private void ListBox1_DoubleClick(object sender, EventArgs e)
{
    if (listBox1.SelectedItem != null)
    {
        const int SW_RESTORE = 9;
        string selected = listBox1.SelectedItem.ToString();
        IntPtr handleOfSelected = getHandle(selected);
        ShowWindowAsync(handleOfSelected, SW_RESTORE);
        SetForegroundWindow(handleOfSelected);

    }
}
 
Share this answer
 
Comments
JaideepChandra 6-Mar-15 23:19pm    
Hi Sir.. sorry to reply late. I did not have internet.
It's not working also.. The selected window from the listbox is not maximizing and is not coming upfront.
I believe the condition "pList.MainWindowTitle.Contains(selectedItem)" does not cover all cases. You may have multiple processes with the selectedItem name in the title, but not all processes may have a window associated with them. Since this is in a foreach loop only the last process handle found will be retrieved.

Also, another important verification would be to see if the process actually has a window associated with it. If MainWindowHandle is 0 there is no window associated with it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900