Frage

I still can't get this to work, nothing appears in the notification bar. This is the full code to minimise, so far:

private void button6_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Minimized;
}

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

private void notifyIcon_Click(object sender, EventArgs e)
{
    this.Show();
    this.WindowState = FormWindowState.Normal;
}

Why isn't this working?

War es hilfreich?

Lösung

You never display anything in the notification area. Trace through your code and try to see what's happening. I've added some comments:

private void button6_Click(object sender, EventArgs e)
{
    // When button 6 is clicked, minimize the form.
    this.WindowState = FormWindowState.Minimized;
}

private void Form_Resize(object sender, EventArgs e)
{
    // Catch the case where the form is minimized, including but not limited to
    // clicks on button 6.
    if (WindowState == FormWindowState.Minimized)
    {
        // In that case, hide the form.
        this.Hide();
    }
}

private void notifyIcon_Click(object sender, EventArgs e)
{
    // If the notification icon is clicked, reshow the form as a normal window.
    this.Show();
    this.WindowState = FormWindowState.Normal;
}

Notice the problem now? When the form is minimized, all you do is hide it. You never tell the NotifyIcon to display its icon! The default value of its Visible property is false. You have to set it to true to make the icon show up, and false to get it to go away.

So modify your code as follows:

private void Form_Resize(object sender, EventArgs e)
{
    // Catch the case where the form is minimized, including but not limited to
    // clicks on button 6.
    if (WindowState == FormWindowState.Minimized)
    {
        // In that case, hide the form.
        this.Hide();

        // And display the notification icon.
        notifyIcon.Visible = true;

        // TODO: You might also want to set other properties on the
        // notification icon, like Text and/or Icon.
    }
}

private void notifyIcon_Click(object sender, EventArgs e)
{
    // If the notification icon is clicked, reshow the form as a normal window.
    this.Show();
    this.WindowState = FormWindowState.Normal;

    // And hide the icon in the notification area.
    notifyIcon.Visible = false;
}

Andere Tipps

Try this code:

private NotifyIcon notifyIcon;
public Form1()
{
    InitializeComponent();

    button6.Click += (sender, e) =>
        {
            this.WindowState = FormWindowState.Minimized;
        };

    this.Resize += (sender, e) =>
        {
            if (WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                notifyIcon.Visible = true;
                notifyIcon.ShowBalloonTip(1000);
            }
        };

    notifyIcon = new NotifyIcon()
        {
            Text = "I'm here!",
            BalloonTipText = "I'm here!",
            Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath)
        };

    notifyIcon.Click += (sender, e) =>
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            notifyIcon.Visible = false;
        };
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top