4

I am trying to get my application to minimize to the notification area and that part is working. The problem is, when I double click it, it is not showing the window again.

This is what I'm doing, I hope it's something simple I'm doing wrong:

public partial class Main : Form
{
    public Main()
    {
        InitializeComponent();
        CreateNotifyIcon();
    }

    private void CreateNotifyIcon()
    {
        mynotifyicon.BalloonTipIcon = ToolTipIcon.Info;
        mynotifyicon.BalloonTipText = "[Balloon Text when Minimized]";
        mynotifyicon.BalloonTipTitle = "[Balloon Title when Minimized]";
        mynotifyicon.Icon = Resources.lightning;
        mynotifyicon.Text = "[Message shown when hovering over tray icon]";
    }

    private void MainLoad(object sender, EventArgs e)
    {
        Resize += MainResize;
        MouseDoubleClick += MainMouseDoubleClick;
    }

    private void MainResize(object sender, EventArgs e)
    {
        try
        {
            if (FormWindowState.Minimized == WindowState)
            {
                mynotifyicon.Visible = true;
                mynotifyicon.ShowBalloonTip(3000);
                ShowInTaskbar = false;
                Hide();
            }

            else if (FormWindowState.Normal == WindowState)
            {
                mynotifyicon.Visible = false;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
    }

private void MainMouseDoubleClick(object sender, MouseEventArgs e)
{
  try
  {
    Show();
    WindowState = FormWindowState.Normal;
    ShowInTaskbar = true;
    mynotifyicon.Visible = false;
  }
  catch (Exception ex)
  {
    MessageBox.Show("Error: " + ex.Message);
  }
}

}

Something I forgot to mention was I put a debug stop on MainMouseDoubleClick and it's never hitting that point.

Thanks for the help!

** EDIT **

I changed the double click to have a try/catch and it's not being reached at all. Not even to the try.

ErocM
  • 4,505
  • 24
  • 94
  • 161
  • Have you set a brakpoint on MainMouseDoubleClick to see if its hitting? If so, any exceptions? – Botonomous Aug 06 '13 at 19:56
  • lol I was in the process of answering that, no hit on it at all. – ErocM Aug 06 '13 at 19:57
  • Where are you attaching the event handler for clicking the notifyicon? – Patrick Aug 06 '13 at 19:58
  • There should be a doubleclick event for the notify icon. You'll need to move the logic you have in MainMouseDoubleClick there. If you dont have one, you can hook in an anonymous delegate. – Botonomous Aug 06 '13 at 19:58
  • @Anon In the initialization "Main". That is being loaded. I put a debut on it and the `MouseDoubleClick += MainMouseDoubleClick;` is loading. – ErocM Aug 06 '13 at 20:00
  • Are you supposed to double click the notifyicon or the form? – Patrick Aug 06 '13 at 20:00

3 Answers3

4

You need to add the click event to the notifyicon. At the moment you are registering the handler on the form.

mynotifyicon.MouseDoubleClick += MainMouseDoubleClick;
Patrick
  • 17,669
  • 6
  • 70
  • 85
1

Attach the MouseDoubleClick event at the end of CreateNotifyIcon:

mynotifyicon.MouseDoubleClick += MainMouseDoubleClick;

I would recommend renaming the event handler MainMouseDoubleClick to something more appropriate and unregistering it from the form's double click event.

Khan
  • 17,904
  • 5
  • 47
  • 59
0

I have used this and its working :

this.WindowState = FormWindowState.Normal;
this.Activate();
Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87