Click here to Skip to main content
15,900,429 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dears,

please, Can anyone tell me how to check Windows forms instance is running at presently?I have Button to display second form,But need to know its already opened or not ?
Posted

To block mulitple instances, you can have a look at this[^]. The code is in C#, but you might be able to convert it to VB.Net.

If the form is a form (application) you coded, you can control the number of instances by using code similar to this[^].

Another useful link at Single Instance Application in VB.NET[^].
 
Share this answer
 
There are a couple of ways:
You could keep a reference to it in your current form class, and check that:
private MyForm myForm = null;
private void OpenNewMyFormInstance()
    {
    if (myForm == null)
        {
        myForm = new MyForm();
        myForm.FormClosed += new FormClosedEventHandler(myForm_FormClosed);
        myForm.Show();
        }
    }
void myForm_FormClosed(object sender, FormClosedEventArgs e)
    {
    myForm = null;
    }
You need to handle the FormClosed event to register that the user closed it.

Or, you could check for an existing instance:
bool isMyFormOpen = false;
foreach (Form f in Application.OpenForms)
    {
    if (f is MyForm)
        {
        isMyFormOpen = true;
        break;
        }
    }
if (!isMyFormOpen)
    {
    MyForm f = new MyForm();
    f.Show();
    }


Me? I'd keep the reference, because I might want to use it for something else.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900