0
votes

How to minimize an application to system tray?

My application is developed in C#.Net. I have written code such that the Form minimizes to system tray if a button is clicked after selecting some settings from comboboxes. The button gets disabled after clicking once. So, I can't use that button to minimize the Form again. The Form pops up when a RichTextBox is populated with data received from COM port. Everything is working fine.

The problem is when the minimized Form pops up after it receives serial data I need an option to minimize it back to system tray again so that it can pop up again when new data is populated in the RichTextBox.

I don't want to add another button to do this whenever Form pops up. Is there any other way to do it?

It would be better if somebody provides code so that the app minimizes to system tray if minimize button in the title bar is clicked.

I just want to know the function name I have to use, something like private Form1_Resize(). Actually, Resize event can't be used because it triggers whenever form is minimized or maximized. I need it to trigger only when Form minimized.

3
You can enable the button again when form popups - Ramashankar
Your only problem is the button being disabled? Just reenable it once the form reappears. - Damir Arh
Do you mean 'minimise to taskbar' or 'hide and show a notification icon'? Task bar icons are the big ones close to the start button, notification icons are the small ones near to the date/time. - Iain Ballard
No, I don't have problem with the code for minimizing to sysTray or popping up. I can't renable the button used initially because it is used to open COM port. So once it is clicked the COM port opens and Form minimizes to SysTray. When data is received and populated in RichTextBox the Form pops up. Once the button is clicked COM port will be open all time so the button (Connect button) will be disabled. I need a method or function which can be used to minimize the app to SysTray when Form's minimize button in title bar is clicked. I just need the event that can be used for this purpose. - user25911
If I press the close button (X button) on title bar the app closes the COM port if it is already open and then exits normally. Why that question? I am not a professional .Net programmer. I am an embedded designer and programmer. Here is the link for my project. How can I improve the code so that it looks more professional? [url]mega.co.nz/… - user25911

3 Answers

1
votes

There are thousand of HowTo's out there, did you try Google? Just one example: http://alperguc.blogspot.de/2008/11/c-system-tray-minimize-to-tray-with.html

0
votes

How about adding a key to minimize the form?

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        this.WindowState = FormWindowState.Minimized;
    }
}
0
votes
private void frmMain_Resize(object sender, EventArgs e)
{
 if (FormWindowState.Minimized == this.WindowState)
{
 mynotifyicon.Visible = true;
 mynotifyicon.ShowBalloonTip(500);
 this.Hide();
}
 else if (FormWindowState.Normal == this.WindowState)
 {
 mynotifyicon.Visible = false;
 }
}