0

I have a simple WinForms program with a toolstrip menu with several buttons (e.g. closing, new profile, etc...), but I want it to minimize on a click of the "move to tray" menu item. I'm using C# on VS 2019 with .NET 3.1, this is my code so far for moving it to the system tray and bringing it back up again from the tray on a double click:

private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                Hide();
                notifyIcon.Visible = true;
            }
        }

private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            this.WindowState = FormWindowState.Normal;
            notifyIcon.Visible = false;
        }

Note that the application only moves to the system tray when the user is manually clicking on the minus-sign or respectively when the resize-event is triggered, but I want it to close on the click on a menu item of a toolstrip menu.

KingOverTheHill
  • 11
  • 1
  • 10
  • 1
    Does this answer your question? [minimize app to system tray](https://stackoverflow.com/questions/7625421/minimize-app-to-system-tray) – Tony Stark Jun 23 '21 at 10:06
  • More or less. The "hiding" part or moving to the tray is not the issue, it's that I want it to happen on a click on a menu strip item. – KingOverTheHill Jun 23 '21 at 10:14
  • Yes so move this code to click event of that menu that's it. – Tony Stark Jun 23 '21 at 10:16
  • @TonyStark I got it, sorry. I'm relatively new to C# and WinForms but didn't know that you could use event handlers in other methods, thank you. But there is a new question: When I try to mention `notifyIcon_DoubleMouseClick`, the arguments I use are `sender` for object and `e` for MouseEventArgs, but VS tells me that the Converting from `System.EventArgs` to `System.Windows.Forms.MouseEventArgs` is not possible? Any ideas to help? – KingOverTheHill Jun 23 '21 at 10:48
  • Yes because System.EventArgs and System.Windows.Forms.MouseEventArgs both are different classes so conversion not possible. – Tony Stark Jun 23 '21 at 10:50
  • But I'm not converting anything, I'm just mentioning `notifyIcon_MouseDoubleClick` in the click event of the menu item like so: `notifyIcon_DoubleMouseClick(sender, e)`. There is no conversion happening, VS just shows me that. Am I overseeing something? Edit: Yes, I used the right `e`, the DoubleMouseClick e and not the other e from `Form1_Resize` – KingOverTheHill Jun 23 '21 at 10:55
  • The mouse click handlers signature is different than the resize signature. Resize requires EventArgs, mouse click requires MouseEventArgs. By passing the EventArgs to the mouse click handler (which expects the type MouseEventArgs), a conversion is required. You should not be calling the event handler in most (if not ALL) cases. In your example you dont use the event args or mouse event args... so instead create new functions and call those from your event handlers. EX. private void ResizeForm() { ... } private void Form1_Resize(object sender, EventArgs e) { ResizeForm(); } – hijinxbassist Jun 23 '21 at 16:41

0 Answers0