So, hello, guys. I'm new to this forums and this problem has been bugging me lately. I'm making a Windows Forms app in C#:

So I have a form with two buttons, one of them is a "New" button (private void botonNuevo_Click(object sender, EventArgs e)).

This new button creates a new Form, this is the code of my 'menu':

namespace EGYP
{
    public partial class menuBienvenida : Form
    {
        private void botonNuevo_Click(object sender, EventArgs e)
        {
            principal principalForm = new principal();
            principalForm.Show();
        }
    }
}

What I want to do, is to make sure is that there can only be one window at a time (the object my "New" button creates). In case it's clicked when there's already an object created, show a Messagebox error, etc.

--

What I wanted to do is to include a counter in my class "menuBienvenida" which is my main form, once the "New" button is clicked and creates the object, increase the counter, and then include a condition in my 'Click' event. Like this:

public partial class menuBienvenida : Form
{
public short counter=0;
private void botonNuevo_Click(object sender, EventArgs e)
        {
if(counter==0)
{
            principal principalForm = new principal();
            principalForm.Show();
            counter++;
}
else
         MessageBox.Show(...);
        }
}

Now the problem is that my other form (the one the "New" button creates) has an exit button, I figured out that I could decrease my counter to 0 again once this button is clicked. But I don't know how to access to my counter from this class.

---

I looked around the code VS generates and Program.cs has something that reads:

static void Main()
        {
         //other code
            Application.Run(new menuBienvenida());
        }

I figure out this is the line that creates my main object (my "menu")but I don't know how to refer to this object, since it doesn't have a name...

---

I would really, really, aprecciate your help if you could give me a hand here, I'm starting to code my first WinForms applications because I only knew how to make Console Apps...

Thanks in advance :)

Use static modifier.

namespace EGYP
{
    public partial class menuBienvenida : Form
    {
       static  principal principalForm=null; 
        private void botonNuevo_Click(object sender, EventArgs e)
        {
           if(principalForm==null)
                principalForm = new principal();
            principalForm.Show();
        }
    }
}

Hey man, thanks for your help, now the button doesn't do anything if there is an active window, if I leave "principalForm" I return to my main menu, but when I click on my "New" button again it gives me this error:

"Cannot access disposed object".

Sorry if I didn't make myself clear on my post. I want the "New" button to not do anything only if there is an active window, once I exit this window, I should be able to create a new one.

Thanks again!

public partial class menuBienvenida : Form
{
private short counter=0;
public short _counter
{
get
{
    return counter;
}
set
{
     counter = value;
}
}
private void botonNuevo_Click(object sender, EventArgs e)
        {
if(counter==0)
{
            principal principalForm = new principal(this);
            principalForm.Show();
            counter++;
}
else
         MessageBox.Show(...);
        }
}

then in principal form mod the constructor to keep track of the main instance.

public partial class principal : Form
{
     Form mMain;
     public principal(Form F);
{
        mMain = f;
       
}
   //create an event handler for on close
   private void principal_onClose(Object sender, EventArgs e)
{
  mMain._counter = 0;
}
}

best of luck.

Modify it.

namespace EGYP
{
    public partial class menuBienvenida : Form
    {
       static  principal principalForm=null; 
        private void botonNuevo_Click(object sender, EventArgs e)
        {
           if(principalForm==null  || t.IsDisposed==true)
                principalForm = new principal();
            principalForm.Show();
        }
    }
}

If a form is shown non-modally using .Show() then the CLR will dispose the form if you don't hang on to a reference. Using adatapost's logic should stop the form from being destroyed and you can also located created forms in Application.OpenForms

Thanks to everyone in this thread, I tried both adatapost's and Diamonddrake's solutions and they worked.

Cheers!

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.