简体   繁体   中英

how do I notify child controls of a change in the parent

what is the best way to keep a child control up to date for changes in the parent.

I have a number of child controls hosted inside my parent object and I can think of two ways to send change information to them.

1) wire the child control to an event in the parent and fire that event on a change in the parent

2) keep a list of the children in an array and iterate through the array when the change has happened and invoke a method in the child to handle the new changes.

I hope I describe it okay.

Both work but there is probably a right way to handle this and a wrong way.

EDIT: below is my code...for event method. I am not happy with how I have wired the child to the parent;any sugestions.

The Parent...

public  class A_Parent       
{

 public delegate void DelUpdateMessage(  string msg );

public event DelUpdateMessage UpdateMessage;
public A_Parent()
{
  a_Child1.prnt = this;
  a_Child2.prnt = this;
  a_Child3.prnt = this;
  a_Child4.prnt = this;
}

private void FireUpdateMessageEvent(  string message)
{
  var handlers = UpdateMessage;

  if (handlers != null)
    handlers(  message );

}
  }

The child...

public  class A_Child        
    {
    A_Parent pnt;
    public A_Parent prnt
    {
     set
       {
        pnt = value;
        pnt.UpdateMessage += new A_Parent.DelUpdateMessage(pnt_UpdateMessage);
        }
     }


void pnt_UpdateMessage(string msg)  {      }

}

Your #1 solution is the best bet, as it allows you to change around your implementation without a change in the parent. Since the parent arguably doesn't care if it has children, the event-based approach is ideal.

UPDATE

The reverse-event approach is disingenuous. People are stating that the parents clearly know what children are interested in them, and I simply tend to disagree with that statement. My designs usually have the children dynamically interested in the parent, and the children subscribing to the events is generally the most favorable way of doing so.

You can find such examples in the .NET model, such as the Page which has events that you can tie into.

Events are not the proper choice here. The parent never has a problem figuring out what child controls need to be notified. Simply add a public method to the UC, like UpdateMessage(). The parent can directly call them.

Reserve events for being able to notify listeners when you have no idea what listeners might be interested. Having a "reverse event" like you're contemplating is unhealthy, it keeps a reference on the form. A form should never be referenced so that it can get garbage collected when it closes. That isn't a real problem here, since the reference is held by a child control, but the principle holds.

Event mechanism is a common routine for such tasks. It will be much more flexible than your second solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM