How to get all the controls on form and non-form?

Y

YXQ

Me.Controls can get all the controls on the form, but i want to get all the
controls include non-from controls, for example ToolTip, ContextMenuStrip...
Thank you
 
K

kimiraikkonen

 Me.Controls can get all the controls on the form, but i want to get all the
controls include non-from controls, for example ToolTip, ContextMenuStrip....
Thank you

I think because they don't inherit Control class, as a result they're
not identified as controls.
See which classes/interfaces they implement. For example ToolTip class
inherits Component class, not control:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx

So, you can loop Components to get them as follows:

For Each comp As System.ComponentModel.Component _
In Me.components.Components
' Tooltip and ContextMenuStrip will be found here
Next

Hope this helps,

Onur Güzel
 
J

Jack Jackson

Because the 'components' field in a form is Private, therefore it is
visible only from within the form.
 
K

kimiraikkonen

You are a expert, thank you, but if this code is in moulde sub, for example:

Public sub AAA(Byval FormName as Form)
    For Each comp As System.ComponentModel.Component In
FormName.components.Components
        ' Tooltip and ContextMenuStrip will be found here
   Next
End Sub

The code above will not work if replace "Me" with "FormName", why?
Thank you

"kimiraikkonen" <[email protected]>
??????:f2c9053a-4fc0-4ac3-b23e-f09df16cd...@g17g2000prg.googlegroups.com....


I think because they don't inherit Control class, as a result they're
not identified as controls.
See which classes/interfaces they implement. For example ToolTip class
inherits Component class, not control:http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip....

So, you can loop Components to get them as follows:

For Each comp As System.ComponentModel.Component _
In Me.components.Components
' Tooltip and ContextMenuStrip will be found here
Next

Hope this helps,

Onur Güzel

Thanks for the feedback,

For the problem, as Jack said, "components" variable supplied with
Private access modifier within that form. To call through other form,
you need to call the loop from other form using "Me".

HTH,

Onur Güzel
 

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