Search code examples
c#minimizetrayicon

C# Minimize to Tray


I know you will be thinking "Not again this question", as I found like a hundred results when I searched for it. But when I put in the code as described on the pages here, it just minimizes to right above the start menu.

This is the code I use (I added a message box to see if the code gets triggered, but the message box never pops up):

private void Form1_Resize(object sender, EventArgs e)
{
    MessageBox.Show("Works1");
    if (WindowState == FormWindowState.Minimized)
    {
        this.Hide();
    }
}

Because I don't know if it links to Form1 or Form, I have tried both, to no avail.

private void Form_Resize(object sender, EventArgs e)
{
    MessageBox.Show("Works");
    if (WindowState == FormWindowState.Minimized)
    {
        this.Hide();
    }
}

Now, when you double click on the Form, it puts this line in the Form1.Designer.cs:

this.Load += new System.EventHandler(this.Form1_Load);

Do I need a similar line to trigger the minimize event?

As you can see, I am completely lost :)

Oh, and it doesn't minimize to the taskbar, as I am using the following code to hide the form on run:

protected override void OnLoad(EventArgs e)
{
    Visible = false; // Hide form window.
    ShowInTaskbar = false; // Remove from taskbar.
    base.OnLoad(e);
}

Solution

  • You need the event

    private void Form1_Resize(object sender, EventArgs e)
    {
    
    }
    

    Creating Event Handlers on the Windows Forms Designer