Components on form

W

Wally

With this cycle

For each ctl in Controls
.....
Next

I can see all visual components on a form but I cannot see Menus,
ContextMenus, ToolTips, Timers, ecc.

How could I write a cycle to see these components. Thanks.

W
 
C

Cor Ligthert

Hi Wally,

The Menus and the Tooltips are no controls in the sence of forms.control
(not derived from)

For a menu you can use by instance this
For Each mn As MenuItem In Me.MainMenu1.MenuItems
'do your stuff
Next

I hope this gives an idea?

Cor
 
J

Jorge

Hi Wally

Herfried suggestion to use System.Reflexion works
perfectly. I've been using the following procedure to
browse all menus/contextMenus of a form.

Public Sub BrowseMenus(ByVal f As Form)

Dim myForm As Type = f.GetType()

Dim fields As FieldInfo() = myForm.GetFields
(BindingFlags.Instance Or BindingFlags.NonPublic)

For Each field As FieldInfo In fields

If field.FieldType.Name = "ContextMenu" Then
Dim menu As ContextMenu = DirectCast
(field.GetValue(f), ContextMenu)
<code here>
End If

If field.FieldType.Name = "MainMenu" Then
Dim menu As MainMenu = DirectCast
(field.GetValue(f), MainMenu)
<code here>

End If

Next
End Sub

Kind Regards
Jorge
 
C

Cor Ligthert

Hi Wally,

To overcome my very simple answer here something more complete sample
(it stays a sample)

\\\
Private Sub Form1_Load(ByVal sender _
As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
testMenu(Me.MainMenu1.MenuItems(0), _
Me.MainMenu1.MenuItems)
testMenu(Me.ContextMenu1.MenuItems(0), _
Me.ContextMenu1.MenuItems)
End Sub
Private Sub testMenu(ByVal mi As MenuItem, _
ByVal mm As Menu.MenuItemCollection)
For Each mi In mm
MessageBox.Show(mi.Text)
testMenu(mi, mi.MenuItems)
Next
End Sub
///

Cor
 
W

Wally

Jorge said:
Hi Wally

Herfried suggestion to use System.Reflexion works
perfectly. I've been using the following procedure to
browse all menus/contextMenus of a form.

Public Sub BrowseMenus(ByVal f As Form)

Dim myForm As Type = f.GetType()

Dim fields As FieldInfo() = myForm.GetFields
(BindingFlags.Instance Or BindingFlags.NonPublic)

For Each field As FieldInfo In fields

If field.FieldType.Name = "ContextMenu" Then
Dim menu As ContextMenu = DirectCast
(field.GetValue(f), ContextMenu)
<code here>
End If

If field.FieldType.Name = "MainMenu" Then
Dim menu As MainMenu = DirectCast
(field.GetValue(f), MainMenu)
<code here>

End If

Next
End Sub

Kind Regards
Jorge
Thank you Herfried and Jorge. With your suggestions my program works fine.

W
 
W

Wally

Cor Ligthert said:
Hi Wally,

To overcome my very simple answer here something more complete sample
(it stays a sample)

\\\
Private Sub Form1_Load(ByVal sender _
As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
testMenu(Me.MainMenu1.MenuItems(0), _
Me.MainMenu1.MenuItems)
testMenu(Me.ContextMenu1.MenuItems(0), _
Me.ContextMenu1.MenuItems)
End Sub
Private Sub testMenu(ByVal mi As MenuItem, _
ByVal mm As Menu.MenuItemCollection)
For Each mi In mm
MessageBox.Show(mi.Text)
testMenu(mi, mi.MenuItems)
Next
End Sub
///

Cor

Thanks Core. Herfried and Jorge already answered to my question.

W
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top