使用反射打开ChildWinforms的常规功能?

时间:2013-09-02 21:19:30

标签: vb.net winforms reflection mdi

我不知道怎么说这个,所以标题看起来有点奇怪。我有一个VB.NET MDI Winform应用程序。在这个应用程序中,当我单击一个menuitem时,会打开一个子窗口。要处理必须打开的子窗口,我在ToolStripMenuItem_Click事件中有case语句。问题是在每个案例项中代码几乎相同。所以我想知道是否可以使用一般功能,以便代码变得更具可读性。

示例:

 Case "mnuPrint"
            Dim ChildWindowFound As Boolean = False

            If Not (MdiChildren.Length.Equals(0)) Then
                For Each ChildWindow As Form In MdiChildren
                    If ChildWindow.Name.Equals("Form1") Then
                        ChildWindow.Activate()
                        ChildWindowFound = True
                    End If
                Next
            End If

            If Not ChildWindowFound Then
                Dim childForm As New Form1()
                childForm.Name = "Form1"
                childForm.MdiParent = Me
                childForm.Show()
            End If
    Case "mnuSearch"
            Dim ChildWindowFound As Boolean = False

            If Not (MdiChildren.Length.Equals(0)) Then
                For Each ChildWindow As Form In MdiChildren
                    If ChildWindow.Name.Equals("Form2") Then
                        ChildWindow.Activate()
                        ChildWindowFound = True
                    End If
                Next
            End If

            If Not ChildWindowFound Then
                Dim childForm As New Form2()
                childForm.Name = "Form2"
                childForm.MdiParent = Me
                childForm.Show()
            End If

正如您所看到的,代码几乎相同但具有不同的形式,这里Form1和Form2不同,但当然可以有许多不同的形式。我正在谈论的功能看起来像是:

 Public Sub OpenNewForm(ByVal frm As Form, ByVal parent As Form, ByVal singleInstance As Boolean)
    Dim ChildWindowFound As Boolean = False

    If Not (parent.MdiChildren.Length.Equals(0)) Then
        For Each ChildWindow As Form In parent.MdiChildren
            If ChildWindow.Name.Equals(frm.Name) Then
                ChildWindow.Activate()
                ChildWindowFound = True
            End If
        Next
    End If

    If Not ChildWindowFound Then
        Dim childForm As New Form
        childForm.Name = frm.Name
        childForm.MdiParent = Me
        childForm.Show()
    End If
End Sub

这不起作用,因为我将参数(前两个)作为Form-type传递,例如,它应该是Form1和ParentForm,而不是Form。我认为这是可能的,只是不知道从哪里开始。可能正在使用反射或什么?

更新:

根据给出的答案,我想出了这个代码,它运行良好:

 'Code contributed by Rod Paddock (Dash Point Software)
'www.dashpoint.com
'Dim oForm As Form = ObjectFactory.CreateAnObject("MyApplication.frmTwo")
'oForm.Show()
Public Shared Function CreateAnObject(ByVal ObjectName As String) As Object
    Dim Assem = [Assembly].GetExecutingAssembly()

    Dim myType As Type = Assem.GetType(ObjectName.Trim)
    Dim o As Object = Nothing
    Try
        o = Activator.CreateInstance(myType)
    Catch oEx As TargetInvocationException
        MessageBox.Show(oEx.ToString)
    End Try

    Return o
End Function

Public Shared Sub ActivateChildWindow(ByVal frmName As String, ByRef ParentMDIWindow As Form)

    Dim ChildWindowFound As Boolean = False
    With ParentMDIWindow
        If Not (.MdiChildren.Length.Equals(0)) Then
            For Each ChildWindow As Form In .MdiChildren
                If ChildWindow.Name.Equals(frmName) Then
                    ChildWindow.Activate()
                    ChildWindowFound = True
                End If
            Next
        End If
    End With

    If Not ChildWindowFound Then
        Dim childForm As Form = CreateAnObject(frmName)
        childForm.Name = frmName
        childForm.MdiParent = ParentMDIWindow
        childForm.Show()
    End If

2 个答案:

答案 0 :(得分:0)

看看这是否能让你入门。创建一个包含三个表单的项目,form1,上面有两个按钮。然后,将此代码添加到Form1:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click
        Dim t As Type
        Select Case sender.name
            Case "Button1"
                t = GetType(Form2)

            Case "Button2"
                t = GetType(Form3)
        End Select

        Dim f = System.Activator.CreateInstance(t)
        f.show()
    End Sub
End Class

答案 1 :(得分:0)

也许这样的事情可以帮到你:

Dim myForm As Form = Nothing
Dim FormName As String = String.Empty
Dim formType As Type

Select Case Options
   Case "mnuPrint"
       FormName = "Form1"
       myForm = getWindowByName(FormName)

   Case "mnuSearch"
       FormName = "Form2"
       myForm = getWindowByName(FormName)
End Select

If myForm Is Nothing Then
   formType = Type.GetType("WindowsApplication2." + FormName) 'WindowsApplication2 is my project's name
   myForm = Activator.CreateInstance(formType)
   myForm.Name = FormName
   myForm.MdiParent = Me
   myForm.Show()
Else
   myForm.Activate()
End If

功能:

 Function getWindowByName(ByVal FormName As String) As Form
        Dim frm As Form = Nothing
        If Not (MdiChildren.Length.Equals(0)) Then
            For Each ChildWindow As Form In MdiChildren
                If ChildWindow.Name.Equals(FormName) Then
                    frm = ChildWindow
                End If
            Next
        End If

        Return frm
    End Function