locked
Use of Delegate maybe? RRS feed

  • Question

  • Problem:
    Parent form needs to be refreshed, this form is not static nor do I want it to be, on this form are child controls that are created dynamically and have user controls that are also built dynamically. Problem is, on the 3rd level down, the user control has the ability to remove a record from a database, now once this record is removed, I need to have the parent form refresh all of its controls. I have a method that handles this, but I need to pass a value from the 3rd level down child control back up to the top main parent, then when this value is placed, the main form needs to then automatically call a method that refreshes the entire form.

    To understand this design:

    Form1
    -usercontrolMain
    --usercontrolChild[collection]
    ---usercontrolGrandchild[collection]

    usercontrolGrandchild->remove object method, thus removing it data from the database
    usercontrolGrandchild->passes up to Form1 the entry ID that built the tree to begin with

    Form1
    Method of ResetPage(string searchValue)
    clear control list in usercontrolMain
    rebuild hierarchy

    I need to acivly monitor a string value on the Form1, when it changes, trigger the ResetPage method.
    Not sure I am approaching this from the right angle.
    Tuesday, August 19, 2008 6:08 PM

Answers

  • I would approach that in terms of events.  A child form would raise an event when something changes.  This can decouple things nicely.

    If you're talking about WebForms and not WinForms, see http://forums.asp.net/
    http://www.peterRitchie.com/blog
    • Marked as answer by Zumwalt Wednesday, August 20, 2008 7:51 PM
    Tuesday, August 19, 2008 6:18 PM
  • You need a parent to refresh when a child does something.  The only component of that that knows it has performed this action.  The child must inform the parent (or the grandparent; but I would keep it to one level.  I.e. inform the parent and let the parent inform its parent).  An event is perfect for this, the parent must subscribe to that event to be informed of it.  A child should never be directly coupled to the parent, the parent should only know about the child.
    http://www.peterRitchie.com/blog
    • Marked as answer by Zumwalt Wednesday, August 20, 2008 7:51 PM
    Tuesday, August 19, 2008 7:02 PM

All replies

  • I would approach that in terms of events.  A child form would raise an event when something changes.  This can decouple things nicely.

    If you're talking about WebForms and not WinForms, see http://forums.asp.net/
    http://www.peterRitchie.com/blog
    • Marked as answer by Zumwalt Wednesday, August 20, 2008 7:51 PM
    Tuesday, August 19, 2008 6:18 PM
  • This is a winforms application, it is not an event I need to raise on the child user control, it is an event I need to raise on the parent.
    The main parent has a child control, that child control has a child control collection, each child control then have a collection of more children controls. Maybe I am misunderstanding your suggestion.

    Are you saying for the usercontrol to raise an event on the main parent form that can then trigger my assembly method?
    If so, where do I look at hooking an event on the main parent control to a child control that is nested within 2 other children controls?
    Tuesday, August 19, 2008 6:56 PM
  • You need a parent to refresh when a child does something.  The only component of that that knows it has performed this action.  The child must inform the parent (or the grandparent; but I would keep it to one level.  I.e. inform the parent and let the parent inform its parent).  An event is perfect for this, the parent must subscribe to that event to be informed of it.  A child should never be directly coupled to the parent, the parent should only know about the child.
    http://www.peterRitchie.com/blog
    • Marked as answer by Zumwalt Wednesday, August 20, 2008 7:51 PM
    Tuesday, August 19, 2008 7:02 PM
  • So, 3 user controls, of which all 3 would need events that each preceding parent should subscribe to, the lowest level fires off the event then all parents up a level chain the event sequence until I get all the way back up to the main parent, then run the method with the variable passed through the event chain, if I am understanding this logic.
    Tuesday, August 19, 2008 7:32 PM
  • Well, here is the problem I am running into, the parent form that has all of the children in it, is not a static class element. What I have tried to do based on your suggestion so far is add an event in second to last user control, but since I am not calling new in my last user control on its parent obviously I can't hook into the event that way, only the parent user control is calling new to create a child user control. All of the documentation I have read so far on events (haven't had a need to create or use them or delegates until now), state that I need to create a delegate, create an event class, create a new instanace of the event class, then fire the event.

    Problem is, the method in the event that I need to use, is actually on the top level form. I have code on the main form that does all of my work to and from a database and resets everything for me. I haven't been able to figure out through MSDN or google a simple event that passes a string value from the child control to its parent without a static field. Do you have a URL to an article that shows a way to pass a value that is created on a user control when a method is called up to its parent, then when the parent realizes it has a new value, pass it along to its parent and so on?

    Every instance of the use for the delegate and event is on static methods, I need to use it against a method that is not static.

    I have tried in the main form to create a delegate as follows:
    public delegate void ResetHandler(string inInfo);  
     
    public partial class frmMain : Form  
    {  
        public event ResetHandler resetNotifier  
        {  
           add  
           {  
               resetNotifier += value;  
           }  
           remove  
           {  
           }  
        }  
    }  
     
    // next I tried  
    public delegate void ResetHandler(string inInfo);  
     
    public partial class frmMain : Form  
    {  
        internal void ResetMarker(string inValue)  
        {  
        // does work I need done  
        }  
    }  
     
    // Then in child user control  
    ResetHandler tmphandler = new ResetHandler(frmMain.ResetMarker);  
    // obviously this throws an error since ReserMarker is not static and I don't want it to be  
     
     


    Here is another version of the attempt:
      public delegate void ResetHandler(string GUIDinfo);  
     
      public partial class frmNewDocument : Form  
      {  
        public static event ResetHandler resetNotifier;  
     
        public frmNewDocument()  
        {  
          InitializeComponent();  
          Data.SetupData();  
          resetNotifier += new ResetHandler(ResetPage);  
     
        }  
    //......  
    // Now, in my usercontrol, I can see frmNewDocument.resetNotifier but I  
    // can't pass it a variable, resetNotifier has a signature that should allow input  
    // of a string like resetNotifier(inString), this is only available at the form level  
    // not in the user control where I need to pass it out of and trigger a reset on the  
    // main form 
    • Edited by Zumwalt Wednesday, August 20, 2008 2:21 PM .. more info
    Wednesday, August 20, 2008 1:19 PM
  • I have now successfully gotten this to fire off the event at the deepest level and pass back the result up the chain to the parent, which then triggered my method as I was desiring it to. Thanks Peter for pointing me in the right direction, just took me a bit of trial and error to get my code to do what I needed.
    Wednesday, August 20, 2008 7:47 PM