954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to Minimize window form on closing?

Can anybody say, how to minimize the window form on close button is clicked in C#?

thanks in advance.

rosebabu
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Can anybody say, how to minimize the window form on close button is clicked in C#?

thanks in advance.


hi u mean, r u created any buton on ur form or r u teling the default close, mini,max button top right corner of form.
tel me clearly where u want to do

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

You need to make use of the FormClosing event. Here is a quick example:

private bool minimize = true;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (minimize)
    {
        e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;
    }
}


The reason for the minimize bool variable is to have control of whether you are closing the Form for reals or not.

vckicks
Junior Poster in Training
58 posts since Jun 2008
Reputation Points: 11
Solved Threads: 9
 

Ah you mean to minimize not close.. now it makes sense.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

The above code not works for me....

Can any one post it correctly?

rosebabu
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Hi,
Which version of .NET Framework you are using? FormClosing is new to .NET Framework 2.0. Or Specify what difficulty you are facing while implementing the above code.

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

What errors do you get? Id suggest make a new winforms app, apply the code there, and if that doesnt work, please post all the code so we can see, as well as the errors if any.. If there are no physical errors, please describe what you're seeing (or not seeing)

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

I thought it goes without saying, but add this in the constructor of your Form:

This.FormClosing += new EventHandler(Form1_FormClosing);

I'm not too sure about the EventHandler part, but the point is attach the event otherwise the code will do nothing.

vckicks
Junior Poster in Training
58 posts since Jun 2008
Reputation Points: 11
Solved Threads: 9
 

i'm using .NET framework 3.5.

and i used the code as

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}

but while clicking close button, the application got closed.

am i using correctly or anything i need to change here?

rosebabu
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

i'm using .NET framework 3.5.

and i used the code as

private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; this.WindowState = FormWindowState.Minimized; } }

but while clicking close button, the application got closed.

am i using correctly or anything i need to change here?

hi im using 2.0 version. if u r using form borderless then u can create own close, min,max button and in close button u can write minmize coding

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

hi rosebabu

u can do one thing may r maynot be correct

in ur form1.designers.csprotected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
//base.Dispose(disposing); //u have to comment this line
}

then it wil work. if u waant to close appl u can use button click event likeApplication.Exit();

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

and u have to use this Form1_FormClosing or Form1_FormClosed

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

Hi it worked for me with simple changes in the code...

in form1.designers.cs

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
//components.Dispose();
// we've to comment the above line and added below line

this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
}
//base.Dispose(disposing); //u have to comment this line
}

and also in form1.cs i did like this

private void Form1_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}

it made the form minimized on closing.

Thanks.

rosebabu
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Hi it worked for me with simple changes in the code...

in form1.designers.cs

protected override void Dispose(bool disposing) { if (disposing && (components != null)) { //components.Dispose(); // we've to comment the above line and added below line

this.WindowState = System.Windows.Forms.FormWindowState.Minimized; } //base.Dispose(disposing); //u have to comment this line }

and also in form1.cs i did like this

private void Form1_FormClosing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; this.WindowState = FormWindowState.Minimized; }

it made the form minimized on closing.

Thanks.

plz mark as solved

Renukavani
Junior Poster
123 posts since Jul 2008
Reputation Points: 10
Solved Threads: 23
 

Use this in the closing event

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
           if (this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                ShowInTaskbar = false;
            }
        }


And then use this in your own closing button click event

private void button1_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            this.Close();
        }
H.A.M
Newbie Poster
1 post since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

not necessary borderless. He has a program with .NET i think. I don't have this problem and I have .NET 3.5. It's weird that it closes. It even works simple like this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
                       
                this.WindowState = FormWindowState.Minimized;
                e.Cancel = true;
        }
Clawsy
Posting Whiz in Training
225 posts since Feb 2008
Reputation Points: 11
Solved Threads: 7
 
this.WindowState = FormWindowState.Minimized;


Thanks, just browsing through.. But this solved my problem as well.

Goalatio
Junior Poster in Training
72 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

I'm glad. Thanks :)

Clawsy
Posting Whiz in Training
225 posts since Feb 2008
Reputation Points: 11
Solved Threads: 7
 

I know this is solved but i felt it really needed to be said:

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
//components.Dispose();
// we've to comment the above line and added below line

this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
}
//base.Dispose(disposing); //u have to comment this line
}

isnot the way to go about it. You will prevent your form and controls from being correctly marked for garbage collection which could result in memory leaks.

If you want to minimise instead of closing use the originally posted code to cancel the FormClosing event and minimise the form instead.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You