tags:

views:

84

answers:

6
+1  Q: 

Working with Forms

Hi,

I'im writing a program that works with 2 forms, the main form and the form where the configuration is made, so when the user clicks toolstripmenu->Preferences the Preferences form is showned and I want it to make the user only capable of having one Preferences form at a time.

When I use:

    Prefs preferencias = new Prefs;

    private void preferenciasToolStripMenuItem_Click(object sender, EventArgs e)   
    {
        preferencias.Show();
    }

It works, but when I close the Preferences form and try to open a new one the program crashes.

And When I use:

    private void preferenciasToolStripMenuItem_Click(object sender, EventArgs e)   
    {
        Prefs preferencias = new Prefs;
        preferencias.Show();
    }

The user can hav multiple Preferences form.

What can I do?

Thanks in advance.

+1  A: 

preferencias.ShowDialog()

will only allow one preference window to be open .

Marcom
+5  A: 

It sounds like you want a modal dialog, so you need to use the ShowDialog( ) method instead of Show( ):

private void preferenciasToolStripMenuItem_Click(object sender, EventArgs e)   
{
    preferencias.ShowDialog();
}
Ed Swangren
A: 

You can use the Application.OpenForms property in your menu item's click event to check if a form of that type is already open. If there is no form of that type open, then you can open your instance. If there is, it simply won't show.

foreach (Form form in Application.OpenForms) {
    if (form.GetType() != typeof(PreferencesForm)) {
        new PreferencesForm().Show();
    }
}

Or as already stated you can call PreferencesForm.ShowDialog() to make the form modal, in which case the user has to close the form before they can even interact with the main form again.

The method you use depends on if you want the user to be able to use the main form even if the preferences form is open.

David Anderson
This seems a little backwards, but it will work if you don't want the preferences dialog to be modal (that does seem like the obvious choice, though)
Adam Robinson
Updated my post with reasoning behind the first method.
David Anderson
A: 

If you're looking for 1 and ONLY 1, You probably want to implement the Singleton pattern for the Prefs class.

moo
I think that is going overboard when the OP simply needs a modal dialog.
Ed Swangren
+2  A: 

The ShowDialog() that others have suggested is a good answer. If you're interested in an alternative, here's something I sometimes do:

private void FormClosing(object sender, FormClosingEventArgs e)
{
     if (e.CloseReason == CloseReason.UserClosing)
     {
        e.Cancel = true;
        Hide();
     }
}

What this does is simply hides the form so that if you show it again, its already loaded into memory. Additionally, if you have a timer or some other thread in that form running, it can still run and do its thing.

Charlie Salts
A: 

Thanks to all of you, the form ShowDialog() worked fine and I'm reading about the singleton pattern now.

Also the FormClosing function is very good.

Thanks.

Ricardo