Get List of all Open Forms in Windows Application

Step 1 : Create new windows form application.
 
Step 2 : Add three win forms as Form1, Form2 and Form3 to application.
 
Step 3 : Design Form3 as follows.
 
 
 Step 4 : Write following code for Form3.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. namespace WindowsFormsApplication1  
  11. {  
  12.     public partial class Form3 : Form  
  13.     {  
  14.         public Form3()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void Form3_Load(object sender, EventArgs e)  
  20.         {  
  21.             label3.Text = Application.OpenForms.Count.ToString();  
  22.             foreach (Form frm in Application.OpenForms)  
  23.             {  
  24.                 label1.Text = frm.Name;  
  25.             }  
  26.         }  
  27.   
  28.         private void button1_Click(object sender, EventArgs e)  
  29.         {  
  30.             Form1 frm1 = new Form1();  
  31.             frm1.Show();  
  32.         }  
  33.   
  34.         private void button2_Click(object sender, EventArgs e)  
  35.         {  
  36.             Form2 frm2 = new Form2();  
  37.             frm2.Show();  
  38.         }  
  39.   
  40.         private void button3_Click(object sender, EventArgs e)  
  41.         {  
  42.             label3.Text = Application.OpenForms.Count.ToString();  
  43.             label1.Text = "";  
  44.             foreach (Form frm in Application.OpenForms)  
  45.             {  
  46.                 label1.Text += frm.Name + "\n";  
  47.             }  
  48.         }  
  49.     }  

Step 5 : Set Form3 as start up form.
 
Step 6 : Run application
 
 
 
 After running application you will get above output. It show the count of open form as 1 and that one form is Form3.
 
 Step 7 : Open Form1 and Form2 by clicking on button Form1 and Form2.
 
 Step 8 : Then click on Get list button to get list of open form as follows 
Ebook Download
View all
Learn
View all