-1

I'm starting my studies in C# and the first goal I defined to myself is writing a small application that starts running on the system tray and shows the CPU temperature on its tip when the user passes the mouse over its icon. I've already found many good questions about what I need (as in here, here, here and here). However, I'd like to do something that I could not find the answer yet.

My application will create its only form during runtime and shows it only if the user clicks in one of the options in the tray icon context menu. It's already working on my project, but I was not able to find a way to hide this form when the user clicks on its minimize button.

I want the application to behave this way: when the user clicks on the form's minimze button, it shall be hidden (and not shown in task bar), but the application shall still be kept running in the system tray.

So, how can I associate a resize or minimize (or whatever) event to a form that is created during runtime?

I'd appreciate any help. Thanks in advance.

P.S.: The form isn't the application's start object, but a customized Application Context.

Edit:

Relevant code excerpt

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
[STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyApplicationContext()); 
    }
}

public class AppForm : System.Windows.Forms.Form
{
    public AppForm()
    {
        this.Size = new System.Drawing.Size(300, 300);
        this.StartPosition = FormStartPosition.CenterScreen;
        this.MaximizeBox = false;
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.Icon = CPUTemp.Properties.Resources.AppIcon;
    }
}

public class MyApplicationContext : ApplicationContext
{
    NotifyIcon trayIcon = new NotifyIcon();
    AppForm frmSetup = new AppForm();

    public MyApplicationContext()
    {
        MenuItem setupMenuItem = new MenuItem("Setup", new EventHandler(ShowSetup));
        MenuItem exitMenuItem = new MenuItem("Exit", new EventHandler(Exit));
(...)
Community
  • 1
  • 1
Marcelo C.
  • 23
  • 7
  • Do you have any code yet? – yellowantphil Feb 20 '16 at 19:30
  • Yes, I do have. But the code I have is fully working. I just don't know how to associate na event to a form that is created during runtime. I use the customized Application context constructor to create the form. Please see question updated with a code excerpt. – Marcelo C. Feb 20 '16 at 19:39
  • Ok, I could disable the minimize button as I did with the maximize one. However, I've already seen some applications running this way (when the user minimizes its form, it hides it and keeps running in the system tray). – Marcelo C. Feb 20 '16 at 19:48
  • Indeed it could be an option, but I don't know how to call it when the user clicks on the form's minimize button. The point is that the form is created during runtime and I don't know how I can associate any code to its minimize (or resize) event in this situation. I believe it may be simple, but I cannot see it... – Marcelo C. Feb 20 '16 at 19:50

2 Answers2

1

Add an event handler for the form's Resize event with this code

if (WindowState == FormWindowState.Minimized)
      Hide();
Simen S
  • 3,210
  • 18
  • 24
0

While waiting for your help, I kept searching for an answer and I just found it. I'd like to share it with you.

You can find detailed explanation on how to create an event handler during runtime and associate it with an object in this link.

Thank you all for your support, specially Siemen s.

Marcelo C.
  • 23
  • 7