取得應用程式已開啟的幾個哪種類別的表單

摘要:取得應用程式已開啟的幾個哪種類別的表單

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        internal class FormA : Form
        {
            internal FormA(int no)
            {
                this.Text = "FormA:" + no.ToString();
                this.Show();
            }
        }

        internal class FormB : Form
        {
            internal FormB(int no)
            {
                this.Text = "FormB:" + no.ToString();
                this.Show();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FormA FA1 = new FormA(1);
            FormA FA2 = new FormA(2);
            FormA FA3 = new FormA(3);

            FormB FB1 = new FormB(1);
            FormB FB2 = new FormB(2);

            foreach (Form f in System.Windows.Forms.Application.OpenForms)
            {
                FormA fa = f as FormA;
                if (fa != null)
                {
                    f.Text += " this is a FormA.";
                }

                FormB fb = f as FormB;
                if (fb != null)
                {
                    f.Text += " this is a FormB.";
                }
            }
        }
    }
}