Results 1 to 3 of 3

Thread: Minimize on Formclosing event

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Minimize on Formclosing event

    hi! how can I hide the form and show a Taskbar icon instead when I click the Close button in the ControlBox. Thanks in advance!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Minimize on Formclosing event

    Code:
    // Indicates whether the form should be minimised rather than closed.
    private bool hideOnClose = true;
    
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (this.hideOnClose)
        {
            // Don't close the form.
            e.Cancel = true;
    
            // Minimise the form.
            this.WindowState = FormWindowState.Minimized;
        }
    }
    
    private void Form1_Resize(object sender, EventArgs e)
    {
        bool minimised = this.WindowState == FormWindowState.Minimized;
    
        // If the form is minimised show only the tray icon, otherwise show the form but not the tray icon.
        this.ShowInTaskbar = !minimised;
        this.Visible = !minimised;
        this.notifyIcon1.Visible = minimised;
    }
    
    protected override void WndProc(ref Message m)
    {
        // You need to change the 0x0 to the value for the message that is received when Windows is shutting down.
        if (m.Msg == 0x0)
        {
            // Windows is shutting down so you must allow the form to close.
            this.hideOnClose = false;
        }
    
        base.WndProc(ref m);
    }
    I don't know the value of the Windows message received when Windows is shutting down but I do know that it has been posted in the C# and/or VB.NET forum before, and probably the VB and API forums too. I'll leave it up to you to track it down, either here or elsewhere on the Web.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Minimize on Formclosing event

    Thanks for that jmcilhinney! you really racks!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width