how to get list all buttons in all forms of the project

醉酒当歌 提交于 2021-02-10 05:34:29

问题


I have the next code to get all forms in the project in 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.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

how to get a list of all buttons in all project's form?


回答1:


Here is how to get every Form in your project: https://stackoverflow.com/a/2364515/1920035

You can use this recursive method to get every control on a Form:

Private Function GetAllControls(root As Control) As  IEnumerable(Of Control)
    Dim children = root.Controls.OfType(Of Control)().ToList()

    For index = children.Count - 1 To 0 Step -1
        children.AddRange(GetAllControls(children(index)))
    Next

    Return children
End Function

Putting it all together, it would look something like this:

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()
Dim allControls = New List(Of Button)()
For Each f In forms
    allControls.AddRange(GetAllControls(f).OfType(Of Button))
Next



回答2:


This should work

    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

edit:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim allForms As IEnumerable(Of Form)
    'get all forms in project
    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

    'get all buttons
    Dim allButtons As New List(Of Button)
    For Each f As Form In allForms
        allButtons.AddRange(IterateControls(f))
    Next

    For Each b As Button In allButtons
        Debug.WriteLine(b.Parent.Name)
    Next
End Sub

Private Function IterateControls(ctrl As Control) As List(Of Button)
    Dim rv As New List(Of Button)
    If TypeOf ctrl Is Button Then 'button?
        rv.Add(DirectCast(ctrl, Button)) 'yes
    Else 'no
        For Each c As Control In ctrl.Controls
            rv.AddRange(IterateControls(c))
        Next
    End If
    Return rv
End Function



回答3:


You can do this with a short recursive function:

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

Call this from the main form code (or wherever you like really):

Dim buttons As List(Of Button) = GetAllButtons(Me.Controls)



回答4:


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


来源:https://stackoverflow.com/questions/66109657/how-to-get-list-all-buttons-in-all-forms-of-the-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!