You must Sign In to post a response.
  • Category: .NET

    Find Active Form in WinForms

    When more than one forms opened, how to find which one is in active state.

    Note: Am not using any mdi stuff.

    FormCollection fc = Application.OpenForms;

    foreach (Form frm in fc)
    {
    //code here to find active form
    }

    Fo example: please assume five forms are opened and each one has textbox. Now I need to find which form has recently selected or where the focus is in textbox.


    Any help must be appreciated.
  • #764183
    FormCollection fc = Application.OpenForms;

    foreach (Form frm in fc)
    {
    //code here to find active form
    }

    Fo example: please assume five forms are opened and each one has textbox. Now I need to find which form has recently selected or where the focus is in textbox.

    Prabu Thangavelu
    Follow me:
    http://prabuthangavelu.blog.com/
    or
    https://twitter.com/prabuthangavelu

  • #764184

    Hai Prabu,
    There is no way to get the current active form as when the form is opened, it will treat it as the active form.
    So when you open a new form, create some variable which will add its value to the array and then with its name or type, you can find that whether it is the container form or the form which is contained in the container.
    Then you can loop through all those active forms and check for your form you are seeking for.
    WPF is having the property IsActive to check for the current form is active or not.
    If you want the form is opened last, you can use Windows internal libraries user32.dll as mentioned in the below link:

    http://www.niteshluharuka.com/how-to-list-the-name-of-current-active-window-in-c/

    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764190
    Hi Pawan,
    Thanks for your help and much appreciated.
    One more question here.
    As per the link what you provided, I can able to get active form.
    But getting null exception error when I try to convert the name of the active form into Form type.
    Have you any idea. Please find the below code I tried and do the needful.
    <Code>
    const int nChars = 256;
    IntPtr handle;
    StringBuilder Buff = new StringBuilder(nChars);
    handle = GetForegroundWindow();
    if (GetWindowText(handle, Buff, nChars) > 0)
    {
    string activeFormName = Buff.ToString();
    Form theActiveForm = Application.OpenForms[activeFormName];
    theActiveForm.Focus();
    //theActiveForm.Show();
    }
    </Code>

    Prabu Thangavelu
    Follow me:
    http://prabuthangavelu.blog.com/
    or
    https://twitter.com/prabuthangavelu

  • #764433
    Hi Prabu,
    Do following:
    FormCollection fc = Application.OpenForms;
    foreach (Form frm in fc)
    {
    if (frm.ContainsFocus)
    {
    Console.WriteLine("Yes");
    }
    else
    {
    Console.WriteLine("No");
    }
    }

    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com


Sign In to post your comments