2
Answers

how to close form 2 from form1 click button in windows form using c#

Photo of Rajadurai

Rajadurai

Jul 19
559
1

Hi Masters,

how to close form 2 from form1 click button in windows form using c#

I have 2 Forms , Form1 and Form 2 

please advise simple method

 

Thanks

Answers (2)

4
Photo of Amit Mohanty
16 52.2k 6.1m Jul 19

Try the below code:

private void Button_Click(object sender, EventArgs e)
{
    Form formToClose = null;

    foreach (Form form in Application.OpenForms)
    {
        if (form is Form2)
        {
            formToClose = form;
            break; // Exit the loop once Form2 is found
        }
    }

    if (formToClose != null)
    {
        formToClose.Close();
    }
}
Accepted
4
Photo of Rajadurai
828 662 4.1k Jul 22

Hi Amit,

 

Its working Thank you so much