Access to Components at Runtime (2) - "Site" object

J

JezB

I'm trying to get at the Components collection given only a reference to a
Form object (things like tooltips, etc, I dont mean the Controls
collection). It has been suggested that I read the Site object and get at
the Container.Components thru that. However, when I evaluate myForm.Site at
runtime this always seems to return a null reference, even though my form
does have components.

Could anyone suggest why and how I work around this?
 
J

JezB

What I've found so far :

You can only get at the components of the form if you have a reference to
one of them (and then use it's Site.Container.Components to get at its
siblings).
 
J

Jorge

Hello J.
I had the same problem i couldn't access ContextMenus on
a form. I found an alternate solution :

Imports System.Reflection

Public Sub teste(ByVal f As Form)

Dim meuForm As Type = f.GetType()

Dim campos As FieldInfo() = meuForm.GetFields
(BindingFlags.Instance Or BindingFlags.NonPublic)

For Each campo As FieldInfo In campos
If campo.FieldType.Name = "ContextMenu" Then
Dim menu As ContextMenu = DirectCast
(campo.GetValue(f), ContextMenu)
Console.WriteLine(menu.ToString)
''do what you want with menu here
End If


Next


End Sub

Kind Regards
Jorge
 
J

JezB

Hi Jorge

I did it in a different way : since my code parses the entire Control tree
of the form object anyway I decided to check, for each control c, test
c.ContextMenu != null and if this is true keep track of it in a hashtable,
keyed by its handle. So at the end of this loop I have all the contextmenu
references. However if you dont want to parse the control tree this may be
more inefficient than your method - but I had this loop anyway.
 

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