我需要将所有表单名称收集到列表中。我尝试了这段代码,但此代码只能找到主文件夹中的表单。这是我的文件夹限制。
以下是我正在使用的代码
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
For Each myType As Type In types
MsgBox(myType.BaseType.FullName)
If myType.BaseType.FullName = "System.Windows.Forms.Form" Then
Dim ProjAndForm = "Mini_Stock_Control." & myType.Name
Dim objType As Type = Type.[GetType](ProjAndForm) '' Get Type Of your string
Dim objForm As Control = DirectCast(Activator.CreateInstance(objType), Control)
MsgBox(objForm.Text)
End If
Next
来自http://www.codeproject.com的参考 有人有解决方案吗?请帮忙(请忽略我的英语不好)
答案 0 :(得分:2)
试试这段代码:
Dim col As New Generic.List(Of Type)
Dim list() As System.Reflection.Assembly = AppDomain.CurrentDomain.GetAssemblies()
For Each asm As Reflection.Assembly In list
Dim types() As Type = asm.GetTypes()
For Each t As Type In types
If t.BaseType Is GetType(Windows.Forms.Form) Then
col.Add(t)
End If
Next
Next
修改后的代码来自:get all form details in vb.net
答案 1 :(得分:0)
我很高兴所有回答我问题的人。但我自己解决了这个问题。 我发现的问题是在行和程序跳过循环中发生错误而没有给出错误。
Dim objForm As Control = DirectCast(Activator.CreateInstance(objType), Control)
所以我做的就是尝试捕获。它工作现在没关系。工作正常。
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
For Each myType As Type In types
If myType.BaseType.FullName = "System.Windows.Forms.Form" Then
Dim ProjAndForm = "Mini_Stock_Control." & myType.Name
Dim objType As Type = Type.[GetType](ProjAndForm) '' Get Type Of your string
Try
Dim objForm As Control = DirectCast(Activator.CreateInstance(objType), Control)
dg.Rows.Add(objForm.Text)
Catch ex As Exception
End Try
End If
Next