Kliver Max
Kliver Max

Reputation: 5299

How to prevent showing form second time?

I calling my form by buttonclick:

var form = new GPZUWizardForm(
    Модуль.ИнформацияОСессии,
    Модуль.IngeoApplication,
    кодВИнгео,
    pzzMapObjectId,
    кодЗУ,
    номерСтраницы,
гпзу);
if (!form.Visible)
   Application.Run(form); 

When i click button second time form showing again.
How to show only one form in once?

Upvotes: 0

Views: 62

Answers (4)

Sergiu
Sergiu

Reputation: 442

IF it is not MDI, iterate through each opened Form to see if it's opened and close it if that's what you need. Here's a quick example of how to do that:

for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
        {
            if (Application.OpenForms[i].Name != "MyFormThatIDon'tWantToClose" && Application.OpenForms[i].Name != "TheOTherFormThatIDontWantToClose" )
                Application.OpenForms[i].Close();
        }

Upvotes: 0

Mohini Mhetre
Mohini Mhetre

Reputation: 919

If you are using MDI parent then before opening any form you need to check whether that form is already open or not. If it is open first close that instance and open new instance.

Upvotes: 0

ViSu
ViSu

Reputation: 482

if form is not null then create a new instance.

Upvotes: 1

gaba
gaba

Reputation: 116

Thats because you are creating a new instance of the form everytime.

You can use the singleton pattern.

http://www.dotnetperls.com/singleton

Upvotes: 2

Related Questions