iam使用vb.net 2008,我有2个列表框,一个用于列出我项目中的所有表单名称并且工作正常,但我需要的是当我单击表单列表时,另一个列表框应该显示我所有的listbox1中所选表单名称中的控件!我一直在努力,没有运气。请帮忙。这是列出所有表格的代码
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
For Each myType As Object In types
If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then
ListBox1.Items.Add(myType.Name)
End If
Next
这就是我在list2中尝试的东西
Dim f As New Form
f.Name = ListBox1.SelectedItem.ToString
For Each c As Control In f.Controls
ListBox2.Items.Add(c.Name)
Next
答案 0 :(得分:0)
OK!经过几个小时的狩猎 - 我希望这可以在vb2008中运行。它适用于vb2015,但我们希望!获取所有表单列表的代码来自其他来源。我在寻找答案时遇到的问题总是导致运行时转换中的错误 - 但最终这似乎有效。希望它也适合你。
Public Class Form1
Dim allforms() As Form
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
allforms = (From t As Type In Me.GetType().Assembly.GetTypes()
Where t.BaseType Is GetType(Form)
Let f = DirectCast(Activator.CreateInstance(t), Form)
Select f).ToArray
For Each frm As Form In allForms
ListBox1.Items.Add(frm.Name)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
For Each c As Control In allforms(ListBox1.SelectedIndex).Controls
ListBox2.Items.Add(c.Name)
Next
End Sub
End Class