VB.net通过窗体名称(字符串)来得到窗体的实例

2008-03-27 10:44:01.0     浏览:1443     来源:e800.net频道
关键词:  VB.net  

方法一(Reflection):

Dim str As String
str = "myTest.Form2" '必须是 命名空间+点+窗体类名
Dim tempAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim frm2 As Form = CType(tempAssembly.CreateInstance(str), Form)
frm2.Show()

方法二(Type.GetType() ):

Dim frmName, fullName As String
frmName = "form2"
fullName = Application.ProductName & "." & frmName
Dim frm2 As Form
frm2 = Activator.CreateInstance(Type.GetType(fullName, True, True))
frm2 .Visible = True