Ostorlabi
Ostorlabi

Reputation: 25

Identify instances of the same form

How could we identify the instance of the same form

This is my problem: I want to show each instance of the same form once only I did:

for (int l = 0; l < 2; l++)
        {
            cameraInstance[l].Start();
            if (cameraInstance[l].MoveDetection == true)
            {
                Formes.CameraViewVS f1 = new Formes.CameraViewVS(cameraInstance[l], adresseIPArray[l]);

                foreach (Form S in Application.OpenForms)
                {
                    if ((S.GetType() == typeof(Formes.CameraViewVS)) && (cameraInstance[l].adresse == f1.IP))  
                    {
                        S.Show();
                        cameraInstance[l].MoveDetection = false;
                        return;
                    }
                }

                                   f1.Owner = this;
                f1.Show();

            }             
        }
Any idea

Upvotes: 0

Views: 146

Answers (1)

Hans Passant
Hans Passant

Reputation: 941218

Something like this, there are not enough details in the question for an exact answer:

            foreach (Form S in Application.OpenForms)
            {
                var frm = S as Formes.CameraViewVS;
                if (frm != null && frm.Addresse == cameraInstance[l].adresse) {
                    // Match, activate it
                    cameraInstance[l].MoveDetection = false;
                    frm.WindowState = FormWindowState.Normal;
                    frm.Activate();
                    return;
                }
            }
            // No match found, create a new one
            var f1 = new Formes.CameraViewVS(cameraInstance[l], adresseIPArray[l]);
            f1.Show(this);

With the assumption that CamerViewVS has a public Addresse property.

Upvotes: 1

Related Questions