Rakesh Rakesh - 2 months ago 28
C# Question

C# Minimize to system tray on close

Hi In my c# application I am trying to minimize application to systems tray, when the form is closed. Here is the code I have tried.

public void MinimizeToTray()
{
try
{
notifyIcon1.BalloonTipTitle = "Sample text";
notifyIcon1.BalloonTipText = "Form is minimized";

if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}


and I am calling the method to form closing event. But the problem is its not minimizing to tray. Its just closing the form.

Answer

Write a event in Form Closing event.

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
        e.Cancel = true;                         
        Hide();
 }

And write using Custom menu strip for notification icon for to show.