开发者

Calling a method on a child form from the parent form

开发者 https://www.devze.com 2023-01-08 01:57 出处:网络
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

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 ParentFo开发者_运维技巧rm 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 UpdateLog method from the parent form

private void UpdateLog(string strFeed)
{
    txtLog.AppendText(strFeed + "\r\n");
    _child.UpdateLog(strFeed);
}

EDIT:

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.


Either Invoke two methods instead of one, or call the childform update method from within the parentform update method, -best in my opinion- add an event to the parentform that gets raised from within the UpdateLogCallback on the parentform. Any classes aware of the parentform can thus piggy back along on the whole update system.


Either sharing one of method from parent and calling it from child or sharing one method from child and calling it from parent may solve it


set form invisible on creation. write following in ctor (just after InitializeComponents()):

Visible = false;

now add a method in child form like this:

internal void ShowForm(Form parent)
{
    var p = parent as Form1;
    if (p != null)
    {
        p.DataReady += (s, ev) =>
        {
            //update your data here
        };
    }
    ShowDialog(parent);
}

in parent form declare an event like this:

internal event EventHandler<EventArgs> DataReady;

depending on your need to pass data to child form, you may need to create custom class handling form EventArgs. Show the child form like this:

var f=new ChildForm();
f.ShowForm(this);//this is parent form
0

精彩评论

暂无评论...
验证码 换一张
取 消