I am creating a user control that uses a communication component. The communication component uses the serial port, so only one instance can exist in a project, typically on the main startup form.
When my user control is added to a form in design mode, I need my user control to find the communication component which can reside on any form in the project.
I can use the following to get the components on the form where the user control is added, but not the other forms in the project:
Me.Parent.Site.Container.Components
I've tried to use reflection, but have only been able to get the list of forms and the controls on the forms, but not the components:
Code:
Dim MyAssembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
Dim MyTypes As Type() = MyAssembly.GetTypes
For Each mType As Type In MyTypes
If mType.BaseType Is GetType(Form) Then
Dim mForm As Form = Activator.CreateInstance(mType)
'*** This only gives me controls, not components
For i1 As Integer = 0 To mForm.Controls.Count - 1
If mForm.Controls(i1).GetType Is GetType(DF1Comm) Then
MsgBox("Found DF1")
End If
Next
mType.InvokeMember("Dispose", Reflection.BindingFlags.InvokeMethod, Nothing, mForm, Nothing)
End If
Next
Can someone tell me how to search (in design mode) all forms in a project for a specific component?
Re: Getting components from all forms in a project
Why does your communication component needs to reside on a form?
to mee it seems better to write a singleton around the component
in a separate project.
Any form or user control that needs the communiction component can use te singleton to retrieve an instance.
This will of course mean that the interface and the logic of this communication component need to be sepparated.
why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
Re: Getting components from all forms in a project
The intention is to allow simple applications to be built using the communication component and user controls without the need for the user to write any code. By having it as a component on a form, the properties can be set via the properties window.
Actually there could be more than one instance of the communication component as long as they all use differerent COM ports. I want the user control to automatically point to the first communication component it finds, but allow the user to change it in the properties window.
I've attached a screen shot to help show what I am describing.