Hey !
Someone can help me how i can close form using another form i try with this code

 private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm = new Form1();

            frm.Close();
        }

but its dont work !!!

Recommended Answers

All 4 Replies

All you have done here in create a new instance of Form1 and then closed that. There is no connection between the Form1 you have open and the new Form1 you created.
You need to pass the reference to Form1 to Form2 when Form2 is created (or at some later point). You can use a property in Form2 to do this.
For example, if both are opened from button clicks in a parent form when the button to create Form2 is clicked the code passes in Form1 to Form2's constructor.
Once you have that reference in Form2 you can call methods on Form1.

Thanks hericles
But how i can pass the reference to Form1 to Form2 when Form2 is created ?

This will work, although normal practise is having a button in the form or dialog itself, to close it.

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        Form Form2; // field inform1 class

        public Form1()
        {
            InitializeComponent();

            // init and show form2
            Form2 = new Form();
            Form2.Text = "Second form";
            Form2.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // in button event handler to close form2
            Form2.Close();
        }
    }
}
commented: Thanks Ur Boss :D +0

Thanks ddanbe maybe that wht i want (y) Its Work !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.