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

Hello,
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);

            myform.Controls.Add(myBtn2);
            myform.ShowDialog();
}

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

Open in new window

Cubic83Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VenkatgviCommented:
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.
0
milindsmCommented:
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;
                this.Close();
            }
        }

0
Death259Commented:
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)
            {
			OpenForm.Close();
      	}
	}
}

Open in new window

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could also do:

        void myBtn2_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            ((Form)btn.Parent).Close();
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cubic83Author Commented:
Hello,

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
Regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.