locked
Trigger event on parent form from child form (windows form - not asp)

    Question

  • Here's what I have...A parent form that opens a child form (update purposes)  I want to be able to trigger an event on the parent form when a button is clicked on the child form.  How do I do this?  Below is a sample of what I'm doing.  Thank you!

    form1//parent form
    ...
    public void button1_Click(object sender, EventArgs e)
    {
    //run some code to load table adapter
    }


    form2//child form
    ...
    private void buttonUpdate_Click(object sender, EventArgs e)
    {
    //run code to update datasource

    ****trigger button1_Click event on parent form******

    //close child form
    }


    Friday, October 16, 2009 3:28 PM

Answers

  • I would change this around, slightly, to accomplish this:

    On the parent form:

    public void button1_Click(object sender, EventArgs e)
    {
        LoadTableAdapter();
    }
    
    private void LoadTableAdapter()
    {
        //run some code to load table adapter
    }
    Then, in your child form, add an Updated event:

    public event EventHandler DatasourceUpdated;
    
    private void buttonUpdate_Click(object sender, EventArgs e)
    {
        //run code to update datasource
    
        if (this.DatasourceUpdated != null)
           this.DatasourceUpdated(this, EventArgs.Empty);
    }
    
    Then, all you need to do, is add one line when you create your child form from within the "parent' form:

    In parent form:

    void RoutineThatCreatesChild() // this can be wherever you construct and show the child...
    {
        ChildForm child = new ChildForm();
        child.DatasourceUpdated += ChildUpdated;
        child.Show();
    }
    
    private void ChildUpdated(object sender, EventArgs e)
    {
        // this will cause this to run now
        LoadTableAdapter();  
    }

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Herzogzwei Friday, October 16, 2009 4:58 PM
    Friday, October 16, 2009 4:22 PM

All replies

  • You could pass a delegate to the constructor of your child form.

    class Form2 : Form
    {
        Action clickIt;

        Form2(Action clickIt)
        {
             InitializeComponent();

             this.clickIt = clickIt;
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            clickIt(sender, e);
        }
    }
    Friday, October 16, 2009 4:18 PM
  • I would change this around, slightly, to accomplish this:

    On the parent form:

    public void button1_Click(object sender, EventArgs e)
    {
        LoadTableAdapter();
    }
    
    private void LoadTableAdapter()
    {
        //run some code to load table adapter
    }
    Then, in your child form, add an Updated event:

    public event EventHandler DatasourceUpdated;
    
    private void buttonUpdate_Click(object sender, EventArgs e)
    {
        //run code to update datasource
    
        if (this.DatasourceUpdated != null)
           this.DatasourceUpdated(this, EventArgs.Empty);
    }
    
    Then, all you need to do, is add one line when you create your child form from within the "parent' form:

    In parent form:

    void RoutineThatCreatesChild() // this can be wherever you construct and show the child...
    {
        ChildForm child = new ChildForm();
        child.DatasourceUpdated += ChildUpdated;
        child.Show();
    }
    
    private void ChildUpdated(object sender, EventArgs e)
    {
        // this will cause this to run now
        LoadTableAdapter();  
    }

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Herzogzwei Friday, October 16, 2009 4:58 PM
    Friday, October 16, 2009 4:22 PM
  • I get an error for Action (Using the generic type 'System.Action<T>' requires '1' type arguments )
    What is the argument for the Action <T> and where is the event from the parent form (button1_click) referenced?
    Friday, October 16, 2009 4:34 PM
  • I get an error for Action (Using the generic type 'System.Action<T>' requires '1' type arguments )
    What is the argument for the Action <T> and where is the event from the parent form (button1_click) referenced?

    If you're going to use this approach (which I don't recommend - the event approach is more in line with how the framework normally implements this pattern), you should use Action<object,EventArgs> instead of Action :

    class Form2 : Form
    {
        Action<object,EventArgs> clickIt;
    
        Form2(Action<object,EventArgs> clickIt)
        { // everything else is the same...


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 16, 2009 4:47 PM
  • Thanks Reed.  That got it.
    Friday, October 16, 2009 4:58 PM