how to limit number of instances of a form being opened?

I have a btnAddNew on my frmMain which opens frmAddNew for adding new service record: What can I do to make sure the no matter how many times the btnAddNew is clicked, the frmAddNew will only have one instance opened. this is the code under btnAddNew_Click:

//open up frmAddNew
frmAddNew frmAddNew1 = new frmAddNew();
frmAddNew1.Show();

Also, I have a btnPendingServices, which opens frmPendingServices, I want to make sure user can only open no more than 3 instances of frmPendingServices. the code under btnPendingServices_Click is:

//open up frmPSV
frmPSV frmPSV1 = new frmPSV();
frmPSV1.Show();

Plz help




Answer this question

how to limit number of instances of a form being opened?

  • DataSort Software

    sorry ssfftt for not answering all your questions, I'm putting in down to a late night early morning. Glad someone else got it sorted for you.

  • Mary Potapova

    Look great ssfftt , only one thing:

    public static int frmPSVOpenedTimes = 0;

    doesn't need to be static, its a meber inside the frmMain, but it will work either way.



  • Loderian

    ok, problem identified.

    if a form is minimized, error will be thrown when "frm.show();" is called

    I solved this problem by adding a variable "frmAddNewStatus"

    public static string frmAddNewStatus= "";

    then within btnAdd_click, if (frmAddNewStatus == "") I just call:

    frmAddNew1.Show();

    else if (frmAddNewStatus == "no"), I do:

    frmAddNew frmAddNew1 = new frmAddNew();
    frmAddNew1.Show();

    then if (frmAddNewStatus == "yes"), I do

    if (frmAddNew1.WindowState != FormWindowState.Normal)
    {
    frmAddNew1.WindowState = FormWindowState.Normal;

    }

    and the frmAddNewStatus is set to "yes" at end of the btnAdd_Click event, set it to "no" within the frmAddNewStatus_Closing event.

    no more drama now, haha



  • Grouchypb

    Derek, thank you very much for correcting my mistake, I will remember it in the future.

    could u plz help more on the major purpose of this thread



  • bpatrick

    hi eligazit thx very much for your help, I just made it , and I did this way:

    //this line is in frmMain

    public static int frmPSVOpenedTimes = 0;

    //this line is in frmMain

    private void btnPSV_Click(object sender, EventArgs e)
    {
    // only allow frmPSV to have 3 instances
    // if more than 3, stop
    if (frmPSVOpenedTimes < 3)
    {
    frmPSVOpenedTimes = frmPSVOpenedTimes + 1;
    frmPSV frmPSV1 = new frmPSV();
    frmPSV1.Show();
    }
    }

    //this line is in frmPSV

    private void frmPSV_FormClosing(object sender, FormClosingEventArgs e)
    {
    frmMain.frmPSVOpenedTimes = frmMain.frmPSVOpenedTimes - 1;
    }

    How does it look



  • John Birch

    hi Derek, another problem happened for the only display a form once:

    at run time, when i click on the button once, it opened frmAddNew, then when I closed it and click on the button again, it threw error:

    can not access a disposed object
    Make sure you have not released a resource before attempting to use it.

    and this error is pointing at this line:

    frmAddNew1.Show();

    plz help



  • Gisela

    thx eligazit, I was thinking of the same solution, but expecting something different/easier/built-in (if there was any), however, it seems the most suitable way for my skill level. cheers~

    btw, you suggested

    private static int _numberOfForms = 0;

    I did this way:

    int numberOfForms = 0;

    what is the difference I am new to C# language.

    I think this variable should be globally accessible, so that other forms can access it within _closing event.



  • Apek

    In your case, I've made it static because I thought you will use this syntax:

    Public class ThreeInstanceClass

    {

    Private static int numberOfInstances = 0;

    {

    Public static ThreeInstanceClass CreateInstance()

    {

    If(numberOfInstances < 3)

    {

    ++ numberOfInstances;

    Return new ThreeInstanceClass();

    }

    Else

    {

    Return null;

    }

    }

    // Ctor is private, use CreateInstance to get a new object

    Private ThreeInstanceClass(){

    }

    }

    }

    The idea is that to create new instace you use the static method:

    ThreeInstanceClass t = ThreeInstanceClass .CreateInstance();

    if(t != null)

    {

    // Do something with t.

    }

    the static counter will pervent any 4th creation of the ThreeInstanceClass instance.

    this is really a quick and dirty example, but this is the idea...



  • Googhum

    Big diff...

    Public and private protected are modifiers to determinate which object can access them:

    public class SomeClass

    {

    private int a = 0;

    }

    Can be access only from inside the SomeClass object.

    public class SomeClass

    {

    publicint a = 0;

    }

    Can be access from any object that 'knows' some SomeClass instance.

    When adding static to the declaration, it means that this variable is not an instance related variable, their is no need to use:

    SomeClass s =new SomeClass();

    s.a = 5;

    But you can call a by this:

    SomeClass.a = 6;

    the a variable is the same for any SomeClass instance you creates, and it is not one variable per instance as the pervious example.

    the public\private apply as well on static members , meaning that :

    private static int a;

    can be called only from inside any SomeClass object by: SomeClass.a = 5;

    and

    public static int a;

    can be called from any where on the project, from some other form i.e:

    public class OtherClass

    {

    public void method()

    {

    SomeClass.a = 6;

    }

    Hope it help....

    }



  • bbanks

    Do not create the form in the button click handler. Instead have it as a member variable of the class. That way rather than creating a new form everytime the button is click you reference the only created form in the class. Like this...

    frmAddNew frmAddNew1 = new frmAddNew();

    btnAddNew_Click {

    frmAddNew1.Show();

    }

    No matter how often the button is pressed it will only reference one created form.



  • flo ben

    ok, thx eligazit for your comment~

    I learnt hips today from you

    all the best



  • Raj Chidipudi

    you got your solution... when calling Show again on a form that is allready shown, nothing happens...

    for 3 instances, I guess the best way is to hold a static counter on that form:

    public partial class Form1 : Form

    {

    private static int _numberOfForms = 0;

    ....

    }

    and for each show of the form increase the index,

    when closing the form decrase it (listen to events FormClose and Visible changed).



  • JonPen

    hi Derek, that's very kind of u, I am still up now, (3:55am), to be able to catchup US time, I am a vampire now~~

    Not every one is as free as me, who enjoys the holiday and learning programming day and nights.

    and I have to say that I like this forum, I get help from every one no matter how silly the question is.

    btw, let's say I created a new thread and posted a new reply myself, can I still delete the entire thread

    Have a good time~



  • how to limit number of instances of a form being opened?