Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close Form c# 1

Status
Not open for further replies.

TotalInsanity

Programmer
Oct 18, 2006
20
0
0
GB
Hey All.

Another rediculous question by me :O)

I have two forms in c# - Form1 & Form2.

I have a button on form 1 which creates an instance of form 2 and shows it with:

Form Form2 = new Form2();
Form2.show();

Now say I have another button.

I want to close the second form in this buttons click event.

I know this sounds bizzare but I would like an example if possible..

How on earth do I do such a thing? Any help greatly appreciated!

 
I explain all in the code:

Code:
namespace WindowsApplication1
{
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Declare here (class scope) a Form2 object (not yet inited)
        Form2 f2;

        private void button1_Click(object sender, EventArgs e)
        {
            // Open a new instance of the Form2
            f2 = new Form2();
            f2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Close Form2 object IF it exists.
            if (f2 != null)
                f2.Dispose();
        }
    }

}
 
You are a star, I was being an idiot, as everything including forms are objects in c# so I must place it in a variable!!

Thanks though for your help :O)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top