How To bring the form at front, on double click of notifyIcon in system tray

Asked By Tejaswini Prashant J
20-Nov-09 07:37 AM
Earn up to 0 extra points for answering this tough question.

Hi,
   In .net windows application, I have NofifyIcon, I want that, when  I will doubkleClick on this, it should bring the form to front.

My code snippets are

  private void myAppIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (ShowChatWindowOnNotify == true && IsWindowMinimized == true)
            {
                // Show();
                this.Instance.Show();
                WindowState = FormWindowState.Maximized;     
                ShowChatWindowOnNotify = false;
                IsWindowMinimized = false;
            }
         
            this.Activate();
            this.BringToFront();
            this.Focus();
      }

Here it's opening the form, but not getting it to the front from the other open windows.
can anybody help me in this?

  Set the .TopMost property on the form

Robbe Morris replied to Tejaswini Prashant J
20-Nov-09 08:49 AM
end of post

  Hi, I tried that also but no success

Tejaswini Prashant J replied to Robbe Morris
20-Nov-09 10:27 PM

Hi can u look into my code,

  private void myAppIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (ShowChatWindowOnNotify == true && IsWindowMinimized == true)
            {
                // Show();
                this.Instance.Show();
                this.Activate();
                WindowState = FormWindowState.Maximized;
                this.Focus();
               // this.BringToFront();
                SetForegroundWindow(this.Handle);
                ShowChatWindowOnNotify = false;
                IsWindowMinimized = false;
                this.TopMost = true;
            }
            else
            {
                this.Activate();               
                this.Focus();
//                this.BringToFront();
                SetForegroundWindow(this.Handle);
                this.TopMost = true;
            }

  [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool SetForegroundWindow(IntPtr hwnd);

  // Property to check and keep open only once instance of chat form.
            public Chat Instance
            {
                get
                {
                    if (Chat._instance != null)
                    {
                        Chat._instance.Close();

                    }
                    Chat._instance = new Chat();
                    Chat._instance.Activate();
                    Chat._instance.Focus();
                    Chat._instance.BringToFront();
                    return Chat._instance;
                }
            }

  I believe you need to set that property prior to the .Show

Robbe Morris replied to Tejaswini Prashant J
21-Nov-09 09:35 AM
And on all new instances of your Chat class
  you'r code is little confusing to me.
[)ia6l0 iii replied to Tejaswini Prashant J
22-Nov-09 04:47 AM
Call this.Activate after the this.Show. that should suffice. If you are losing the focus for some other reason, add the this.Focus() after those two statements

btw, it looks like you have modified the instance properties and also the other properties. 
Create New Account