Question

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);
}
Était-ce utile?

La solution

You need the event

private void Form1_Resize(object sender, EventArgs e)
{

}

Creating Event Handlers on the Windows Forms Designer

Autres conseils

  1. Add a NotifyIcon component to your Form. Make sure you set an icon via the properties pane otherwise it will be invisible.
  2. Create an event handler for the form's Control.SizeChanged event. In that event handler place the following code:

sample code:

private void MainForm_SizeChanged(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
        ShowInTaskbar = false;
}

And then to make the form visible again the NotifyIcon.MouseDoubleClick event handler you can place the following code:

private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
    WindowState = FormWindowState.Normal;
    ShowInTaskbar = true;
}

The basic thing you need to know is events. Events are triggered when certain things happen to your form (or any control). For example, when the form is resized, or loaded, or clicked, an event is raised. You can hook into this event to execute your own code when the event happens.

In your case you want to execute code to minimize the form, on the event that the form is resized. So you need to hook your method to the resize event. The name of your method is not relevant, so let's use a better name:

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

To hook your HideWhenMinimized method into the Resize event of the form, you have to do it like this:

this.Resize += new System.EventHandler(this.HideWhenMinimized);

If you add that line of code in the form's constructor or Load event, then your code gets called as soon as the form is resized.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top