Hi. I'm working with my project right now. In my project, there will be 2 form; I will call form1 and form2. Form 1 call first and then it has button to call form 2. I don't want user open many form2 so I use a private boolean variable. When the button is clicked, it call the function to check the bool variable, if false then show form 1 and set the bool variable to true. And form2 open, and that works to prevent user open many form. But after that, I cannot call it again due to the bool variable that time is true. So i want like when the form closed, it will set the variable to false, but it doesn't work. I use debugger and find out that when I close the form, it will call dispose to erase every resource, so that the instance of the form1 in form2 is deleted. So how can I fix that? All I want is prevent it from open many and open it again when need. I appreciate every answer.

Some of the code you are using might be helpful, but using the Close event is probably what you are looking for.

I don't want user open many form2 so I use a private boolean variable. When the button is clicked, it call the function to check the bool variable, if false then show form 1 and set the bool variable to true. And form2 open, and that works to prevent user open many form

What about using ShowDialog() instead of Show() without using any variables it'll enforce user to not open more than 1 instance of form2

Instead of using a private bool variable, use a private variable for Form2, then you have some options...

public partial class Form1 : Form
    {
        private Form2 _form2 = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            _form2 = new Form2();
            _form2.FormClosed += new FormClosedEventHandler(_form2_FormClosed);
            _form2.Show();
        }

        private void _form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            _form2.Dispose();
            _form2 = null;
            button1.Enabled = true;
        }
    }

Simple, just let the Form2.FormClosed event take care of telling Form1 that it has closed, and that takes care of the _form2 var, and the button.

// Jerry

Thank Jerry it works, but I have something to say to Ramy. The way you show me, especially it will work to prevent user open another form, but that way it just start on 1 thread, so if i want to interact with 2 form, i have to write a code to describe another thread. I also try FormClosing but noting happen, i thought it has to be formClosing because it happens before the dispose method.

Did you mention you work with multithreading technique?
Please mark it as solved.

Thank u guys, the steps and the code which u have shared :it helped me alot.

Thank u friends for giving me this good code..this thread will help me a lot

Jerry's solution worked perfect. Thanks for this great resource. I am following Daniweb on twitter now.

Jerry's solution worked perfect. Thanks for this great resource. I am following Daniweb on twitter now.

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.