0

我有一个项目,其中有几个基于安全级别启用/禁用的项目。我正在尝试遍历表单上的所有控件以获取它们的名称并生成一个列表。我可以获得控件及其子项的所有名称,但它没有找到我拥有的所有上下文菜单。在设计期间,我将所有安全项目命名为 Sec_???。这是我到目前为止的代码。它将找到控件的名称并将其添加到列表中。如果它是绑定导航器,它将搜索菜单项并添加任何具有 Sec 标记的菜单项。我如何为我的所有上下文菜单做同样的事情?

Public Sub ProcessControls(ByVal ctrlContainer As Control)
    For Each ctrl As Control In ctrlContainer.Controls
        If ctrl.Name.ToString.StartsWith("Sec") Then
            FileOpen(1, "Sec_names.txt", OpenMode.Append)
            PrintLine(1, "**********")
            PrintLine(1, ctrl.Name.ToString & "," & ctrl.GetType.ToString)
            FileClose(1)
        End If
        If TypeOf ctrl Is BindingNavigator AndAlso ctrl.Name.ToString.StartsWith("Sec") Then
            Dim mnuName As BindingNavigator = CType(ctrl, BindingNavigator)
            For i = 0 To mnuName.Items.Count - 1
                Try
                    Dim mnu As ToolStripButton = CType(mnuName.Items(i), ToolStripButton)
                    If mnu.Name.ToString.StartsWith("Sec") Then
                        FileOpen(1, "Sec_names.txt", OpenMode.Append)
                        PrintLine(1, mnu.Name.ToString & "," & mnu.GetType.ToString)
                        FileClose(1)
                    End If
                Catch ex As Exception

                End Try
            Next
        End If
        ' recursively call this function for the control's children
        If ctrl.HasChildren Then
            ProcessControls(ctrl)
        End If
    Next
End Sub

编辑: ProcessControls(Me) 是我用来启动进程的。

4

1 回答 1

0

尝试使用

For Each vComponent In Me.components.Components
    'Determine if it is component you want process
    'do something with it
Next

确保它不是空的。为什么 Me.Components 什么都没有?

于 2012-08-02T18:22:52.030 回答