반응형

#region 프로젝트 폼 목록 읽어 오기

        public static Form GetAssemblyForm(string strFormName)
        {
            Form f = null;
            foreach (Type t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
            {
                if (t.Name == strFormName)                     //프로젝트 내 폼 중에서 찾을 이름과 같으면...
                {
                    object o = Activator.CreateInstance(t);    //인스턴스 개체 생성 
                    f = o as Form;                                  //인스턴스 개체 폼 형식으로 캐스팅
                }
            }
            return f;
        }

        public static List

 GetAssemblyFormList()
        {
            List
 ltForm = new List

();
            foreach (Type t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
            {
                if (t.BaseType.FullName.ToString() == "System.Windows.Forms.Form")  // 타입이 윈도우즈 폼이면...
                {
                    object o = Activator.CreateInstance(t);                                      //개체 생성
                    Form f = o as Form;                                                            //폼 형식으로 캐스팅
                    ltForm.Add(f);                                                                    //리스트에 담기
                }
            }

            return ltForm;

        }

        #endregion

 

 

 

 

소스 예제...

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;

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


        #region 프로젝트 폼 목록 읽어 오기

        private Form GetAssemblyForm(string strFormName)
        {
            Form f = null;
            foreach (Type t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
            {
                if (t.Name == strFormName)
                {
                    object o = Activator.CreateInstance(t);
                    f = o as Form;
                }
            }

            return f;

        }

        private List<Form> GetAssemblyFormList()
        {
            List<Form> ltForm = new List<Form>();

            listBox1.Items.Clear();

            foreach (Type t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
            {
                if (t.BaseType.FullName.ToString() == "System.Windows.Forms.Form")
                {
                    object o = Activator.CreateInstance(t);
                    Form f = o as Form;
                    ltForm.Add(f);
                }
            }

            return ltForm;

        }

        #endregion


        private void button1_Click(object sender, EventArgs e)
        {
            List<Form> ltList = GetAssemblyFormList();

            for (int iCount = 0; iCount < ltList.Count; iCount++)
            {
                Form f = ltList[iCount] as Form;
                listBox1.Items.Add(f.Text);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form f = GetAssemblyForm("frmSearch");

            MessageBox.Show(f.Text + " 를 찾았습니다.");
        }
    }
}

 

반응형

+ Recent posts