0

I have a parent form FormSubscribingToTheEvent and a child form FormThatThrowsTheEvent The child form has a button. When the button is clicked on the child form, I wish the parent form to be notified and the MessageBox to show the message: "The Close button was clicked."

I found the post: "Pass click event of child control to the parent control" Pass click event of child control to the parent control and I followed Reza Aghaei's instructions, which is the only and accepted answer.

Unfortunately, I get the error message: "An unhandled exception of type 'System.StackOverflowException' occurred in PassClickEventOfChildControlToTheParentControl.exe"

Code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace PassClickEventOfChildControlToTheParentControl
    {
        public partial class FormSubscribingToTheEvent : Form
        {
            public FormSubscribingToTheEvent()
            {
                InitializeComponent();

                FormThatThrowsTheEvent instanceOfFormThatThrowsTheEvent = new FormThatThrowsTheEvent();
                instanceOfFormThatThrowsTheEvent.CloseButtonClicked += new EventHandler(instanceOfFormThatThrowsTheEvent.CloseButton_Click);
                instanceOfFormThatThrowsTheEvent.Show();
            }

            private void InstanceOfFormThatThrowsTheEvent_CloseButtonClicked(object sender, EventArgs e)
            {
                MessageBox.Show("The Close button was clicked.");
            }
        }
    }    
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace PassClickEventOfChildControlToTheParentControl
    {
        public partial class FormThatThrowsTheEvent : Form
        {
            public event EventHandler CloseButtonClicked;

            public FormThatThrowsTheEvent()
            {
                InitializeComponent();
            }

            public void CloseButton_Click(object sender, EventArgs e)
            {
                OnCloseButtonClicked(e); // !!! An unhandled exception of type 'System.StackOverflowException' occurred in PassClickEventOfChildControlToTheParentControl.exe
            }

            /// <summary>
            /// To raise the XXXX event, it is enough to invoke the XXXX event delegate.
            /// the reason for creating the protected virtual OnXXXX is just to follow the pattern to let the derivers override the method 
            /// and customize the behavior before/after raising the event.
            /// </summary>
            /// <param name="e"></param>
            protected virtual void OnCloseButtonClicked(EventArgs e)
            {
                CloseButtonClicked.Invoke(this, e); // !!! An unhandled exception of type 'System.StackOverflowException' occurred in PassClickEventOfChildControlToTheParentControl.exe
            }
        }
    } 

Please explain what I am doing wrong and how I can correct the code so that the parent form gets notified of the button click event on the child form and the MessageBox to show the message: "The Close button was clicked."

Ade Stringer
  • 2,611
  • 17
  • 28
user2102327
  • 59
  • 2
  • 6
  • 19
  • `OnCloseButtonClicked` invokes the `CloseButtonClicked` event, which is handled by `OnCloseButtonClicked`, which invokes the `CloseButtonClicked` event, which is handled by `OnCloseButtonClicked`, which invokes the `CloseButtonClicked` event, which is handled by `OnCloseButtonClicked`, which invokes the `CloseButtonClicked` event, which is handled by `OnCloseButtonClicked`, etc. My suggestion: don't use `instanceOfFormThatThrowsTheEvent.CloseButton_Click` to handle the click event. – ProgrammingLlama Mar 16 '21 at 06:45
  • I'm closing your question as a duplicate of the one you referenced instead of voting to close it as a typo. The answer you referenced doesn't (at any point) suggest that you handle the event using the same method that invokes the event. This is only ever going to cause an infinite recursive loop, leading to a StackOverflowException. Instead, it suggests the main form should contain a handler for the event you create. – ProgrammingLlama Mar 16 '21 at 06:50
  • @Llama As you can infer from my question, I am not familiar with the subject. The code is the best I could come up from the instructions in the post. I did not at any time imply that these instructions are incorrect. I am sorry to confess that from your comments, with my knowlege, I am unable to fix my code. If it is not too great an effort for you, please post a few lines of code, so I can understand and learn frrom your answer. Thank you in advance. – user2102327 Mar 16 '21 at 07:56
  • Change `instanceOfFormThatThrowsTheEvent.CloseButtonClicked += new EventHandler(instanceOfFormThatThrowsTheEvent.CloseButton_Click);` to `instanceOfFormThatThrowsTheEvent.CloseButtonClicked += new EventHandler(InstanceOfFormThatThrowsTheEvent_CloseButtonClicked);` so that you're actually handling the event with the method you intend to (presumably). – ProgrammingLlama Mar 16 '21 at 08:00

0 Answers0