How to automatically switch and show between MDI child windows on a parent MDI in c# visual studio?

Asked

Viewed 28 times

-1

I have an application that when it loads the main form (parent), it automatically creates two children Forms and show maximized, I use the following code in the Parent form to create the two Forms:

       for (int i = 0; i < 2; i++)
        {
            frmSinotico sin = new frmSinotico(Par1, i+Par2);
            sin.MdiParent = this;
            sin.Show();
        }

But I have to put in the Main form (parent) a timer that changes the display of these two Formularies. I used Formcollecion on the timer to load the open Formularies and to see if they exist.

        FormCollection formAbertos = Application.OpenForms;
        foreach (Form frms in formAbertos)
        {
            string nome = frms.Text ;
            var formAb = Application.OpenForms[nome];
        }
        Application.OpenForms[formAb ].Focus();

However it gives error when I try to show the Focus in the name of the return form.

There’s some other way to do it?

1 answer

0

Which error is showing up? You declared the variable within the iteration foreach, and is trying to access it outside.

Another thing, you guarantee that the Text form will be exactly the same as the Nome form? I believe that it would not be a good practice to use in this way.

What you could try is to give the Focus inside the same iteration, just check if the form is different from the parent form, and if it already has the Focus.

FormCollection formAbertos = Application.OpenForms;
foreach (Form frms in formAbertos)
{
     //ignora se o form for o Pai
     if(frms is FormPai)
     {
         continue;
     }
     
     //verifica se ele ja está com o focus
     if (!frms.Focused)
     {
         frms.Focus();
         //interrompe
         break;
     }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.