Skip to content

How to allow only one instance of a form?

Drew Taylor

Asked Jul 3, 2007, 2:40 AM EDT6 answers
Share
LinkedInX

Hello,

I have a main form which displays information which the user can update by clicking an update button, this creates a form with text fields and a submit button which updates a text file when the user clicks the button.

However, the user can click the update button on the main form as many times as they want and it creates lots of the smaller form! Is there a way to allow the smaller form to be displayed while the main form is behind it, but only allow one instance of the form even if the user clicks the button again?

6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Declare the smaller form as variable in your main form.

    If the smaller form has to be shown, check if that variable isn't null - if it does just reset properties in that form to defaults and show that one, otherwise set that variable by creating an instance and then show it.

    0
  • 0

    That sounds difficult! But I'm guessing it's not, could you show me how you would do it in code?

    0
  • 0


    assign one Reference of small form in Main form. Before calling or creating instance of small form, check whether it is null of not.If it is null that mean its instance has not created yet,otherwise it is null.

    0
  • 0
    I usually use MDI when I want to create an app that deals with multiple forms. It makes it easier to relate them and what not. I actually had a boolean function that I built to check and see if an MDI Child form already exists and it works very well.
    0
  • 0

    You could just disable the Update button when it's clicked
    then re-enable it when the 2nd Form closes

    e.g

    public partial class MainForm: Form
    {
    //... RAD Code

    private void UpdateButton_Click(object sender, EventArgs e)
    {
     UpdateButton.Enabled = false;// disable button
     Form2 UpdateForm = new Form2(); // create 2nd Form
     UpdateForm.FormClosing += new FormClosingEventHandler(this.UpdateForm_FormClosing);
     // attach event handler
     UpdateForm.Show(); // shows the form
    }

    private void UpdateForm_FormClosing(object sender, EventArgs e)
    {
       UpdateButton.Enabled = true;// enable button
    }

    }

    0