반응형

#Region "프로젝트 폼 목록 읽어 오기..."


    '프로젝트 내 폼 찾기...
    Public Shared Function GetAssemblyForm(ByVal strFormName As String) As Form
        Dim f As Form = Nothing

        For Each t As Type In System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
            If (t.Name = strFormName) Then                       '찾을려는 폼 이름과 같으면....
                Dim o As Object = Activator.CreateInstance(t)   '새로운 인스턴스 개체 생성...
                f = CType(o, Form)                                     '개체를 Form 개체로 변환...
                Exit For
            End If
        Next

        Return f                                                           '찾았는 폼 개체 반환 Nothing 이면 못찾음 아니면 찾음
    End Function


  '프로젝트 내 폼 리스트 반환...
    Public Shared Function GetAssemblyFormList() As List(Of Form)
        Dim ltForm As List(Of Form) = New List(Of Form)

        For Each t As Type In System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
            If (t.BaseType.FullName.ToString() = "System.Windows.Forms.Form") Then ' 개체 타입이 Form 이면...
                Dim o As Object = Activator.CreateInstance(t)       '새로운 인스턴스 개체 생성...
                Dim f As Form = CType(o, Form)                       '개체를 Form 개체로 변환...
                ltForm.Add(f)                                                '리스트변수에 찾은 Form 개체 담기...
            End If
        Next

        Return ltForm                                                       '폼 리스트 반환...
    End Function

#End Region

 

 

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ltList As List(Of Form) = GetAssemblyFormList()

        For iCount As Integer = 0 To ltList.Count - 1

            Dim f As Form = CType(ltList(iCount), Form)
            ListBox1.Items.Add(f.Text)

        Next


    End Sub

    Public Shared Function GetAssemblyForm(ByVal strFormName As String) As Form
        Dim f As Form = Nothing

        For Each t As Type In System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
            If (t.Name = strFormName) Then
                Dim o As Object = Activator.CreateInstance(t)
                f = CType(o, Form)
                Exit For
            End If
        Next

        Return f
    End Function

    Public Shared Function GetAssemblyFormList() As List(Of Form)
        Dim ltForm As List(Of Form) = New List(Of Form)

        For Each t As Type In System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
            If (t.BaseType.FullName.ToString() = "System.Windows.Forms.Form") Then
                Dim o As Object = Activator.CreateInstance(t)
                Dim f As Form = CType(o, Form)
                ltForm.Add(f)
            End If
        Next

        Return ltForm
    End Function


End Class
반응형

+ Recent posts