-2

I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried

    private void SystemParametersClick(object sender, EventArgs e)
    {
        Xpan sp = new Xpan();

            sp.CurrentItem = this.GetCaller(sender);
            if (sp.Visible==false)
            {
                sp.Show();  
            }

    }  

It doesnt work because it is not the same instance. :(
How do i make it only pop once?

Soner Gönül
  • 94,086
  • 102
  • 195
  • 339
woolford
  • 287
  • 1
  • 7
  • 12

3 Answers3

0

Maybe this simple approach would suffice?

private bool has_been_shown = false;

private void SystemParametersClick(object sender, EventArgs e)
{
    if(!has_been_shown)
    {
        has_been_shown = true;
        Xpan sp = new Xpan();
    }
}  
Andrius Naruševičius
  • 7,983
  • 6
  • 48
  • 74
0

First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.

private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    Hide();
}

Then define your Xpan form as a class member of the parent form, e.g.:

private readonly Xpan _sp = new Xpan();

And finally defile your Click handler this way:

private void SystemParametersClick(object sender, EventArgs e)
{
    if (!_sp.Visible)
    {
        _sp.Show();
    }
    else
    {
        _sp.Activate();
    }
}

That's it.

Ronnix
  • 310
  • 4
  • 12
0

Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.

public class MainForm : Form
{
    private Xpan _Xpan;

    public MainForm()
    {
        InitializeComponent();
        _Xpan = new Xpan();
    }

    private void SystemParametersClick(object sender, EventArgs e)
    {
        _Xpan.Show();
    }
}
Oliver
  • 41,265
  • 7
  • 89
  • 144