none
Activate() and Focus() the windows form if it was already opened. RRS feed

  • Question

  • hi all,

    I am coming across a stubborn issue in here. 

    Let's say i have 3 forms in my windows form project, frmA, frmB, and frmC, whereby frmA is the main form.

    frmA, frmB and frmC can be switched to each other by using frm.Show(), now what i want is if a frm is already opened, then rather than open a new form, i would like to re-focus on the opened form. 

    below are the c# code that i used:

    Code in frmA:

            private void frmBToolStripMenuItem_Click(object sender, EventArgs e)
            {
                bool isFormOpened = false;
                frmB frm_b = new frmB();
                clsHelper.IsFormOpen(typeof(frmB), out isFormOpened);
                if (isFormOpened == true)
                {
                    frm_b.Activate();
                }
                else
                {
                    frm_b.Show();
                }
                this.Hide()
            }


    code in frmC:

    private void frmBToolStripMenuItem_Click(object sender, EventArgs e)
            {
                bool isFormOpened = false;
                frmB frm_b = new frmB();
                clsHelper.IsFormOpen(typeof(frmB),out isFormOpened);
                if (isFormOpened == true)
                {
                    frm_b.Activate();
                }
                else
                {
                    frm_b.Show();
                }
            }

    Code in clsHelper class:

    internal static void IsFormOpen(Type formType, out bool isFormOpen)
            {
                isFormOpen = false;
                foreach (Form form in Application.OpenForms)
                {
                    if (formType.Name == form.Name)
                    {
                        isFormOpen = true;
                    }
                }
            }

    now the code in frmC is able to find that the frmB already opened (isFormOpen returns true), hence it goes to frm_b.Activate(), and i am expecting that frmB will be Focused, but actually it wasn't, instead the frmC was still be Focused, did i miss anything in here plz. 

    many thanks.

     


    • Edited by AndieDu Wednesday, April 18, 2018 8:24 AM
    Wednesday, April 18, 2018 8:21 AM

Answers

  • Have you tried BringToFront() or setting TopMost property of that form to true:

    See the following post:

    https://stackoverflow.com/questions/5282588/how-can-i-bring-my-application-window-to-the-front

    Hope it helps.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by AndieDu Friday, April 20, 2018 1:59 AM
    Wednesday, April 18, 2018 8:54 AM
  • thanks Ehsan, you did point to the right direction.

    below is the ultimate solution of scenario, hope it helps in case anyone else had the similar issue.

    Methods definition in class Helper:

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Windows.Forms;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ExampleNamespace
    {
        class clsHelper
        {
            
    
            internal static Form IsFormOpen(Type formType, out bool isFormOpen)
            {
                Form targetedForm = new Form();
                isFormOpen = false;
                foreach (Form form in Application.OpenForms)
                {
                    if (formType.Name == form.Name)
                    {
                        isFormOpen = true;
                        targetedForm = form;
                    }
                }
                return targetedForm;
            }
    
            internal static void MenuSwitching(ToolStripMenuItem menuItem)
            {
                bool isFormOpened = false;
                if (menuItem.Name == "frmA")
                {
                    Form targetedForm = clsHelper.IsFormOpen(typeof(frmA), out isFormOpened);
                    if (isFormOpened == true)
                    {
                        targetedForm.Activate();
                        targetedForm.BringToFront();
                    }
                    else
                    {
                        frmA a_form = new frmA();
                        a_form.Show();
                    }
                }
                else if (menuItem.Name == "frmB")
                {
                    Form targetedForm = clsHelper.IsFormOpen(typeof(frmB), out isFormOpened);
                    if (isFormOpened == true)
                    {
                        targetedForm.Activate();
                        targetedForm.BringToFront();
                    }
                    else
                    {
                        frmB b_form = new a_form();
                        b_form.Show();
                    }
                }
            }
        }
    }
    

    method implementation from Forms:

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ExampleNamespace
    {
        public partial class frmA : Form
        {
            private void fmA_Click(object sender, EventArgs e)
            {
                clsHelper.MenuSwitching(frmA);
            }
    
            private void frmB_Click(object sender, EventArgs e)
            {
                clsHelper.MenuSwitching(frmB);
            }
        }
    }
    

    • Marked as answer by AndieDu Friday, April 20, 2018 1:59 AM
    Friday, April 20, 2018 1:58 AM

All replies

  • Have you tried BringToFront() or setting TopMost property of that form to true:

    See the following post:

    https://stackoverflow.com/questions/5282588/how-can-i-bring-my-application-window-to-the-front

    Hope it helps.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by AndieDu Friday, April 20, 2018 1:59 AM
    Wednesday, April 18, 2018 8:54 AM
  • thanks Ehsan, you did point to the right direction.

    below is the ultimate solution of scenario, hope it helps in case anyone else had the similar issue.

    Methods definition in class Helper:

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Windows.Forms;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ExampleNamespace
    {
        class clsHelper
        {
            
    
            internal static Form IsFormOpen(Type formType, out bool isFormOpen)
            {
                Form targetedForm = new Form();
                isFormOpen = false;
                foreach (Form form in Application.OpenForms)
                {
                    if (formType.Name == form.Name)
                    {
                        isFormOpen = true;
                        targetedForm = form;
                    }
                }
                return targetedForm;
            }
    
            internal static void MenuSwitching(ToolStripMenuItem menuItem)
            {
                bool isFormOpened = false;
                if (menuItem.Name == "frmA")
                {
                    Form targetedForm = clsHelper.IsFormOpen(typeof(frmA), out isFormOpened);
                    if (isFormOpened == true)
                    {
                        targetedForm.Activate();
                        targetedForm.BringToFront();
                    }
                    else
                    {
                        frmA a_form = new frmA();
                        a_form.Show();
                    }
                }
                else if (menuItem.Name == "frmB")
                {
                    Form targetedForm = clsHelper.IsFormOpen(typeof(frmB), out isFormOpened);
                    if (isFormOpened == true)
                    {
                        targetedForm.Activate();
                        targetedForm.BringToFront();
                    }
                    else
                    {
                        frmB b_form = new a_form();
                        b_form.Show();
                    }
                }
            }
        }
    }
    

    method implementation from Forms:

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ExampleNamespace
    {
        public partial class frmA : Form
        {
            private void fmA_Click(object sender, EventArgs e)
            {
                clsHelper.MenuSwitching(frmA);
            }
    
            private void frmB_Click(object sender, EventArgs e)
            {
                clsHelper.MenuSwitching(frmB);
            }
        }
    }
    

    • Marked as answer by AndieDu Friday, April 20, 2018 1:59 AM
    Friday, April 20, 2018 1:58 AM