我想實現類似於Java中Class.ForName(XXX)這個功能。具體說明如下
我現在有一個變量存放Form的名稱,然後我想根據這個變量直接判斷該調用哪個Form的ShowDialog方法
如果無法根據名字獲取的話,我只能這樣寫:
Select Case vFormName
Case "frm001"
frm001.ShowDialog
Case "frm002"
frm002.ShowDialog
...
那有沒有某種方法可以實現如下:
DirectCast(XXXXXX(vFormName), Form).ShowDialog
補充說明:不能用new的形式,比如 vForm = new XXXX(vFormName),因為項目中有功能要求是要全局使用frm001這個東西的。因為是改造項目,所以不能更改這種處理邏輯。
哪位知道,請指教,謝謝啦!
Public Class MyForm
Inherits System.Collections.CollectionBase
Private Lists() As FormIndexAndName
Default Public ReadOnly Property Item(ByVal Name As String) As System.Windows.Forms.Form
Get
Dim i As Int32
For i = 0 To Lists.Length - 1
With Lists(i)
If .Name = Name Then
Return Me.List.Item(.Index)
End If
End With
Next
Return Nothing
End Get
End Property
Public Sub AddItem(ByVal btnItem As System.Windows.Forms.Form, Name As String)
ReDim Preserve Lists(Lists.Length)
Lists(Lists.Length).Name = Name
Lists(Lists.Length).Index = Me.List.Count
Me.List.Add(btnItem)
End Sub
Private Structure FormIndexAndName
Dim Index As Int32
Dim Name As String
End Structure
Sub New()
ReDim Lists(-1)
End Sub
End Class
這個應該能解決你的問題