In my C# application I have an option dialog that can be opened from a menu command.
I want to ensure that the option dialog have only one instance (user cannot open more than one option window at a given time) without making it modal.
Also if the user already have this window opened, and he clicks in the menu item to open it again, the app just makes the already visible form became the top most window.
Can anyone point me directions on how accomplish these tasks?
Thank you very much.
A Single Instance application is an application that limits the program to run only one instance at a time. This means that you cannot open the same program twice.
" This feature is already built in to Windows Forms. Just go to the project properties and click the "Single Instance Application" checkbox. "
Well, the simplest way is to have a static field which stores a reference to the single instance or null, and then a method to retrieve it or create a new one.
Note that this isn't the same as making it a singleton - because I assume if the form is closed, you'd want to create a new instance next time. (The alternative - hiding it and reusing it - is shown in STO's answer.) You may want something like this:
public class OptionsDialog : Form
{
private static OptionsDialog openForm = null;
// No need for locking - you'll be doing all this on the UI thread...
public static OptionsDialog GetInstance()
{
if (openForm == null)
{
openForm = new OptionsDialog();
openForm.FormClosed += delegate { openForm = null; };
}
return openForm;
}
}
You may want to make the method perform the "bring it to the front" steps as well, of course.
You need to prevent the form from closing. If you don't, the form will be disposed and becomes unusable. You can do this by implementing the FormClosing event:
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
this.Hide();
e.Cancel = true;
}
}
To make it a singleton, just keep track of the life time of the form in your main form class:
frmOptions options;
private void btnShowOptions_Click(object sender, EventArgs e) {
if (options == null) {
options = new frmOptions();
// To make absolutely sure:
options.FormClosed += (o, ea) => options = null;
}
else {
options.WindowState = FormWindowState.Normal;
}
options.Show();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With