I have 2 forms ParentForm and a child form. In my parent form I have a thread listener which listens to a feed which updates an area of the ParentForm. Now, I have a ChildForm which also needs the data from the listener to be placed on an area of the ChildForm. the thread listener uses delegate to update my ParentForm when it gets a feed.
My ParentForm has these.
private delegate void UpdateLogCallback(string strFeed);
private Thread thr;
private void InitializeFeed()
{
...
// Get the feed connection
...
thr = new Thread(new ThreadStart(ReceivedFeeds));
thr.Start();
}
private void ReceivedFeeds()
{
string strFeed = GetFromStream();
// invoke my updater while connected
while(Connected)
{
this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { strFeed });
}
}
private void UpdateLog(string strFeed)
{
txtLog.AppendText(strFeed + "\r\n");
}
this works fine, now here’s the question. When I open a ChildForm from the ParentForm, I also want to update a centain part of that form using what I get from the ReceivedFeeds() in my ParentForm how will I achieve this? I can’t create another feed connection in the ChildForm since this will duplicate the Connection and cause an error. I just want to do the same as what UpdateLog() will do in the ChildForm.
Edit
I’m calling the ChildForm to open on an OnClick event on the parent form and show it.
// onclick event
ChildForm childForm = new ChildForm();
childForm.Name = ((ListBox)sender).SelectedItem.ToString();
childForm.ShowDialog(this);
This is how I open my ChildForm and how do I call the methods inside the ChildForm in my UpdateLogCallback or in my UpdateLog()
I also have an UpdateLog() method in my ChildForm.
If you simply hold a reference to the child form from within your parnet form you can call the
UpdateLogmethod from the parent formEDIT:
Also, if you have many child forms you can have a collection of them. Just make sure you remove them from the list when they close.