Windows Forms - Get List of all Open Forms
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
---------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
label3.Text = Application.OpenForms.Count.ToString();
foreach (Form frm in Application.OpenForms)
{
label1.Text = frm.Name;
}
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
private void button3_Click(object sender, EventArgs e)
{
label3.Text = Application.OpenForms.Count.ToString();
label1.Text = "";
foreach (Form frm in Application.OpenForms)
{
label1.Text += frm.Name + "\n";
}
}
}
}
-------------------------------------------------------------------------------------------------------
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
No comments:
Post a Comment