Vb.net 如何获取项目所有形式中的所有按钮列表
我有下一个代码,可以在vb.net中获取项目中的所有表单Vb.net 如何获取项目所有形式中的所有按钮列表,vb.net,Vb.net,我有下一个代码,可以在vb.net中获取项目中的所有表单 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly() Dim types As Type() = myAssembly.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
For Each t As Type In types
If UCase(t.BaseType.ToString) = "SYSTEM.WINDOWS.FORMS.FORM" Then
MessageBox.Show(t.Name)
End If
Next
End Sub
如何获取所有项目表单中所有按钮的列表?以下是如何获取项目中的每个表单: 可以使用此递归方法获取窗体上的每个控件:
私有函数GetAllControls(root作为控件)作为IEnumerable(Of控件)
Dim children=root.Controls.OfType(Of Control)(.ToList)()
对于索引=子项。计数-1到0步骤-1
AddRange(GetAllControls(children(index)))
下一个
返回儿童
端函数
把所有这些放在一起,看起来像这样:
Dim formType As Type=Me.GetType().BaseType
Dim forms As List(Of Form)=(从Me.GetType().Assembly.GetTypes()中的t开始,其中t.IsSubclassOf(formType)=True选择DirectCast(Activator.CreateInstance(t),Form)).ToList()
Dim allControls=新列表(按钮)()
对于表单中的每个f
AddRange(GetAllControls(f.)(按钮的类型))
下一个
这应该行得通
Dim allForms As IEnumerable(Of String)
allForms = From t As Type In Me.GetType().Assembly.GetTypes()
Where t.BaseType Is GetType(Form)
Select t.Name
For Each frmName As String In allForms
MessageBox.Show(frmName)
Next
编辑:
您可以使用一个简短的递归函数:
Function GetAllButtons(controls As Control.ControlCollection) As List(Of Button)
Dim buttons As List(Of Button) = New List(Of Button)
For Each c As Control In controls
Select Case True
Case c.Controls.Count > 0
buttons.AddRange(GetAllButtons(c.Controls))
Case TypeOf c Is Button
buttons.Add(DirectCast(c, Button))
End Select
Next c
Return buttons
End Function
从主表单代码(或您真正喜欢的任何地方)调用:
Dim formType As Type=Me.GetType().BaseType
Dim forms As List(Of Form)=(从Me.GetType().Assembly.GetTypes()中的t开始,其中t.IsSubclassOf(formType)=True选择DirectCast(Activator.CreateInstance(t),Form)).ToList()
对于表单中的每个f
Dim ctrl As Control=f.GetNextControl(Me,True)
直到ctrl不起作用为止
如果ctrl的类型为Button,并且ctrl.Name为“cmdClose”,则
ctrl.Enabled=False
如果结束
ctrl=f.GetNextControl(ctrl,True)
如果ctrl是空的,那么
后藤1号线
其他的
如果ctrl.GetType=GetType(按钮),则
MsgBox(f.Text&“&”ctrl.Name)
如果结束
如果结束
第1行:
环
下一个
目标是什么?这是用于某种源代码审计还是在运行时实际需要信息?如果它是源代码审计类型的东西,您可以将源文件解析为文本,然后找到按钮类定义的实例。有子表单吗?比如,表单中的表单(表单中)?这里您给出了获取所有表单的代码,但我需要的是获取每个表单中的按钮form@Hamada添加了获取所有按钮的代码。我可以得到更多解释吗,我尝试了该方法,但我不知道如何正确循环所有按钮声明formType
获取表单的基本类型(System.Windows.Forms.Form).Declarationforms
获取程序集中的每个表单。DeclarationallControls
声明一个新列表,以仅存储作为按钮的控件。在For Each中,它使用GetAllControls
递归获取每个控件,但Type方法的表示您只需要作为按钮的控件。Dim formType As Type=Me.GetType().BaseType Dim forms As List(Of Form)=(从Me.GetType().Assembly.GetTypes()中的t开始,其中t.IsSubclassOf(formType)=True选择DirectCast(Activator.CreateInstance(t,Form)).ToList()Dim allControls=forms allControls.AddRange(GetAllControls(f.)Of Type中的每个f的新列表(Of Button)()(按钮的)i为整数=0到allControls.Count-1 MsgBox(allControls(i.Text)接下来我添加了msgbox以获取按钮的名称,如何获取表单的名称?您能建议更多帮助吗please@Hamada请停止在评论中发布代码。我认为这是一些非常普通的代码:完全不必要的GoTo
,冗长的布尔判断,等等。这不是一个好的解决方案。
Function GetAllButtons(controls As Control.ControlCollection) As List(Of Button)
Dim buttons As List(Of Button) = New List(Of Button)
For Each c As Control In controls
Select Case True
Case c.Controls.Count > 0
buttons.AddRange(GetAllButtons(c.Controls))
Case TypeOf c Is Button
buttons.Add(DirectCast(c, Button))
End Select
Next c
Return buttons
End Function
Dim buttons As List(Of Button) = GetAllButtons(Me.Controls)
Dim formType As Type = Me.GetType().BaseType
Dim forms As List(Of Form) = (From t In Me.GetType().Assembly.GetTypes() Where t.IsSubclassOf(formType) = True Select DirectCast(Activator.CreateInstance(t), Form)).ToList()
For Each f In forms
Dim ctrl As Control = f.GetNextControl(Me, True)
Do Until ctrl Is Nothing
If TypeOf ctrl Is Button AndAlso ctrl.Name <> "cmdClose" Then
ctrl.Enabled = False
End If
ctrl = f.GetNextControl(ctrl, True)
If ctrl Is Nothing Then
GoTo Line1
Else
If ctrl.GetType = GetType(Button) Then
MsgBox(f.Text & " _ " & ctrl.Name)
End If
End If
Line1:
Loop
Next