[Webinar] Streamline your web hosting managementRegister Today


CSharp - How can I close a form that has been created on runtime?

Posted on 2010-03-25
Medium Priority
Last Modified: 2013-12-17
I am looking for a way to close a form that I created on runtime. I have a button "OpenForm" with the OpenForm_Click Handler. This method creates the form "myform" with another button "myBtn2". When I click this second button "myform" should close. So I created the handler mybtn2_Click.  

But now I am stuck. How can I access myform in this method? Can it be pushed out of the "sender" or do I need to use a completly different way?
private void OpenForm_Click(object sender, EventArgs e)
            Form myform = new Form();
            myform.Width = 200;
            myform.Height = 120;
            myform.FormBorderStyle = FormBorderStyle.FixedSingle;
            myform.MaximizeBox = false;
            myform.MinimizeBox = false;

            //  .... 

            Button myBtn2 = new Button();
            myBtn2.Text = "Close";
            myBtn2.Left = 90;
            myBtn2.Top = 55;
            myBtn2.Click += new EventHandler(this.myBtn2_Click);


void myBtn2_Click(object sender, EventArgs e)
               // The code to close myform

Open in new window

Question by:Cubic83

Assisted Solution

Venkatgvi earned 800 total points
ID: 28647952
After creating the button control , that is below the Code
Button myBtn2 = new Button();

add the below code

myform.CancelButton = myBtn2;

this will solve the purpose.

Assisted Solution

milindsm earned 200 total points
ID: 28648932
You can declare myform as a member of your main class so it will be accessible by both btn1 as well as btn2 handler.

If anyway you need user interference for closing the form one preferable approach could be, Handle KeyPress event in newly created form. If the key pressed is escape key (ESC), close the form something like this,

       void Form2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            if (e.KeyChar == (char)System.Windows.Forms.Keys.Escape)
                e.Handled = true;


Assisted Solution

Death259 earned 100 total points
ID: 28679889
You can re-use the following method to close any form. Just feed the Form Type in as a parameter, so if this the form you want to close is Form1 (the default form created by visual studio) then you could do CloseForm(Form1).
public void CloseForm(Type FormType)
	foreach (Form OpenForm in System.Windows.Forms.Application.OpenForms)
      	if (OpenForm.GetType() == FormType)

Open in new window

LVL 86

Accepted Solution

Mike Tomlinson earned 900 total points
ID: 28691925
You could also do:

        void myBtn2_Click(object sender, EventArgs e)
            Button btn = (Button)sender;

Author Comment

ID: 28746045

thank you. All of your comments were helpfull.

@Venkatgvi: In my case - your solution was the fastest, so I took your snipped.
@milindsm: I implemented your code too as it is very cool feature
@Death259: This is somehow overkilled.
@Idle_Mind: I expected a method like yours and I think there are often cases where you need to change something on your forms.

Thank you

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question