How to maximize a minimized form on button click

Introduction:

In my window based application, there was a problem which became a bit difficult to me. The problem was that, I was showing another form ( Like form2) on button click on form1. After minimizing form2 When I click button on form1, it again open new window for form2.(due to code which was written for showing form2).

Code on form1:

private
void btnok_Click(object sender, EventArgs e)
        {
            Form2 obj = new Form2();
            obj.Show();
        }

Run the application.
Output:



When I click ok button, it show new window for form2.

To solve this problem write the following code.

namespace
myapplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Form2 obj = new Form2();
        private void btnok_Click(object sender, EventArgs e)
        {
          
            if (obj.WindowState == FormWindowState.Minimized)
            {
                obj.WindowState = FormWindowState.Maximized;
            }
            else
            {
                obj.Show();
            }
        }
    }
}

Run the application. You will note that, if form2 is minimized and you click "ok" button on form1, then no new window will be open (as form2). Only form2 will be maximized.

Ebook Download
View all
Learn
View all