none
Get a list of Window Names RRS feed

  • Question

  • Hi,
    I have an program that when multiple windows are opened, it will open as "Session A", Session B, Session C and so on..
    Here's my scenario:
    Sessions A, B, C are running and B completes its operation. So, now there's just A & C running. After a while another instance kicks in and now opens as Session B since that's not running. How do I capture that it's the Session B which opened and run my instance in Session B?

    Thank you
    Abhi

    Friday, September 21, 2018 6:00 PM

Answers

  • Hi h_abhijith,

    Thank you for posting here.

    For your question, you could try the code below. I use a timer. Every 2 seconds get the list of Windows Names again.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsForms
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Form2 F2 = new Form2();
                F2.Show();
                Form3 F3 = new Form3();
                F3.Show();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
                timer1.Interval = 2000;
                timer1.Tick += Timer1_Tick;
                timer1.Start();
            }
            FormCollection list;
            int count;
            private void Timer1_Tick(object sender, EventArgs e)
            {
                StringBuilder stringbuilder = new StringBuilder();
                list = Application.OpenForms;
                if (count != list.Count)
                {
                    foreach (var item in list)
                    {
                        stringbuilder.Append(item).Append(",");
                    }
                    count = list.Count;
                    MessageBox.Show(stringbuilder.ToString());
                    
                }         
    
            }     
    
        }
    }
    
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by h_abhijith Monday, September 24, 2018 1:54 PM
    Monday, September 24, 2018 7:31 AM
    Moderator

All replies

  • If this is a windows forms app then see the following along with perhaps a timer.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.application.openforms?view=netframework-4.7.2

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/app-development/how-to-get-all-windows-in-an-application


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, September 21, 2018 6:19 PM
    Moderator
  • Hi h_abhijith,

    Thank you for posting here.

    For your question, you could try the code below. I use a timer. Every 2 seconds get the list of Windows Names again.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsForms
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Form2 F2 = new Form2();
                F2.Show();
                Form3 F3 = new Form3();
                F3.Show();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
                timer1.Interval = 2000;
                timer1.Tick += Timer1_Tick;
                timer1.Start();
            }
            FormCollection list;
            int count;
            private void Timer1_Tick(object sender, EventArgs e)
            {
                StringBuilder stringbuilder = new StringBuilder();
                list = Application.OpenForms;
                if (count != list.Count)
                {
                    foreach (var item in list)
                    {
                        stringbuilder.Append(item).Append(",");
                    }
                    count = list.Count;
                    MessageBox.Show(stringbuilder.ToString());
                    
                }         
    
            }     
    
        }
    }
    
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by h_abhijith Monday, September 24, 2018 1:54 PM
    Monday, September 24, 2018 7:31 AM
    Moderator
  • Thank you Wendy!
    Monday, September 24, 2018 1:54 PM