Problem with Form appearing from MouseClick on NotifyIcon

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Wiebe
    New Member
    • Mar 2011
    • 66

    Problem with Form appearing from MouseClick on NotifyIcon

    The following code makes the Form(menu) appear, and disappear, but if I select a opened window or anything besides the Form(menu) or NotifyIcon, the Form(menu) disappears and will not come back from clicking the NotifyIcon. I would much appreciate if someone could give me a solution to this problem, otherwise there isn't really a point to continue building my program.

    Code:
            
    
            public static bool menuOpen = false;
            public static int mouseUpXPosition;
            public static Menu menu;
    
            public StartUp()
            {
                InitializeComponent();
                SystemTrayIcon = new NotifyIcon();
                SystemTrayIcon.Icon = Properties.Resources.Icon;
                twSystemTrayIcon.MouseUp += new MouseEventHandler(this.ShowHideIcon_MouseUp);
                SystemTrayIcon.Visible = true;
                menu = new Menu();
            }
    
            private void ShowHideIcon_MouseUp(object sender, MouseEventArgs e)
            {
                switch(e.Button)
                {
                    case MouseButtons.Left:
    
                    if (menuOpen == false)
                    {
                        if (MousePosition.X < Screen.PrimaryScreen.WorkingArea.Width - (menu.Width / 2))
                        {
                            mouseUpXPosition = MousePosition.X;
                            menu.Location = new Point(mouseUpXPosition - menu.Width / 2, Screen.PrimaryScreen.WorkingArea.Height - menu.Height);
                            menu.Show();
                            menuOpen = true;
                        }
                        else
                        {
                            menu.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - menu.Width, Screen.PrimaryScreen.WorkingArea.Height - menu.Height);
                            menu.Show();
                            menuOpen = true;
                        }
                    }
                    else
                    {
                        menu.Hide();
                        menuOpen = false;
                    }
    
                    break;
                }
            }
    The Form, that is called StartUp, is invisible to everyone, and this code that is placed in the StartUp Form, is only to show and hide the Form called Menu.

    I'm pretty sure nothing from my Form called Menu is causing the problem, but if you think seeing that code would help, let me know.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I usually just use menu.Visible=!m enu.Visible to toggle the window. I'm not sure what all the other stuff yo uare doing is all about

    Comment

    • Tyler Wiebe
      New Member
      • Mar 2011
      • 66

      #3
      If your reply of menu.Visible was meant to replace the if(menuOpen == false), I appreciate it, I feel rather stupid not thinking of it. And actually, well thinking of other ways of describing my problem, I found my solution, which is

      TopMost = true;
      TopMost = false;

      Simple, effective, and it gets the job done. Thank you though for your advice.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I wasn't really clear on what you problem was.

        An example of what i do would be like this:
        [code=C#]
        private void ShowHideIcon_Mo useUp(object sender, MouseEventArgs e)
        {
        if(e.Button==Mo useButtons.Left )menu.Visible=! menu.Visible;
        }
        [/code]
        I don't mess around with window position though.

        Comment

        • Tyler Wiebe
          New Member
          • Mar 2011
          • 66

          #5
          Okay, I see what you mean. But I have actually found out my fix, didn't fix everything.

          When my program starts, my Menu Form stays in the same spot of all programs, so lets say I open two other programs, those will be in front of my Menu Form, if I click the NotifyIcon, it shows and hides, but it doesn't come in front of the other programs. With the

          TopMost = true;
          TopMost = false;

          It comes to the front like I want, but if it was open, it needs to be double clicked, so that it will close, and then open on top.

          Is there any event that will activate when my application is not the top window?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Why not leave topmost always true and then just show/hide the window?

            Comment

            • Tyler Wiebe
              New Member
              • Mar 2011
              • 66

              #7
              All I want is it to hide when it's not in use. Like when you click the volume icon, the menu appears, but when you select anything else, it hides.

              Comment

              Working...