Sending data from Parent to multiple child forms

B2Ben

Well-known member
Joined
Aug 17, 2006
Messages
52
Programming Experience
Beginner
Hey all -

I've been developing a project with one main parent form that watches the COM port for commands. When a command comes in, I'd like to pass along that data to one or many child forms. I have my parent and child forms built, but I need to make them interact.

Here's the basic idea of what I'm trying to accomplish...

ParentForm has an array of ChildForms

Each ChildForm has a public variable called "Address"

So...
- A command comes in off the COM port
- ParentForm parses it, and extracts the "Address"
- ParentForm loops through its array of child forms

For each childform in the array...
-- If the child's Address variable is the one we want, activate Child's "ProcessCommand" function using the command that just came in
Next

If a Child form wants to send back a command over the COM port, it should be able to access a function in the ParentForm to send out that command on the COM line.

Basically, the parent form handles all the COM port communications. The child forms will process a command if it is meant for them, and the child forms tell the parent form when to send a command back out on the COM port.

...I hope that all made sense. I'm learning more and more with each VB.NET project I tackle, but I still have trouble getting my forms to talk to each other... and especially using multiple instances of child forms in this case. Any help would be appreciated.
 
Post up a bit of your code - primarily the methods on the ParentForm which send and receive messages from the child forms.

I dont know how youve implemented your setup, but since the send COM method is universal you could make it Shared and then call it from anyway like this:

VB.NET:
ParentForm.SendCOM("message goes here")

You dont actually need the 'object' reference to the child form's parent, the parent form merely houses a method we can call.

mafro
 
I built a separate project just to experiment with form-to-form communication, and I found a pretty easy way using owner/ownedform variables... Here's basically what I came up with:

PARENT FORM:

Create new child/owned form:

Private Sub NewChild_Click
Dim child As New FrmChild
child.Show()
child.Owner = Me
End Sub

Send message to all owned forms via owned form’s function:

Dim frm As FrmChild
For Each frm In Me.OwnedForms
frm.Receive("Hello Child")
Next

CHILD FORM:

Send message back to owner form via owner’s function:

Sub Respond(ByVal message As String)
Dim prnt As Form1
prnt = Me.Owner
prnt.ReceiveMsg(message)
End Sub
 
Back
Top