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

thanks in advance.

Recommended Answers

All 18 Replies

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

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.

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

The above code not works for me....

Can any one post it correctly?

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.

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)

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.

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

hi rosebabu

u can do one thing may r maynot be correct

in ur form1.designers.cs

protected 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 like

Application.Exit();

and u have to use this Form1_FormClosing or Form1_FormClosed

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

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();
        }

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;
        }
this.WindowState = FormWindowState.Minimized;

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

I'm glad. Thanks :)

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
}

is not 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.