我有一个项目,其中包含几个基于安全级别启用/禁用的项目。我试图遍历表单上的所有控件来获取其名称并生成列表。我可以获得控件及其子代的所有名称,但它没有找到我所有的上下文菜单。在设计过程中,我已将所有安全项目命名为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)是我用来启动这个过程的。
答案 0 :(得分:0)
尝试使用
For Each vComponent In Me.components.Components
'Determine if it is component you want process
'do something with it
Next
确保它不是空的。Why is Me.Components Nothing?