Button Listener C#

I have made a custom control that is a groupbox and has a button (called btnListenToMe) in it. I add lots of these controls to a form (call this frmLayout) at runtime. Now in another form (called frmLaunch) i push a button which opens frmLayout and frmLayout is populated with many of my custom controls.

Now what I want is everytime a btnListenToMe is pushed (remember there are multiple btnListenToMe's on the layout) I want a flag to be thrown to frmLaunch saying a button has been clicked
npl77Asked:
Who is Participating?
 
YiogiConnect With a Mentor Commented:
Actually here is the code anyway, I'm just copy pasting from one of my own projects and renaming the variables:

In you custom control:
        public delegate void SampleDelegate(string sampleParameter); // You can also pass parameters if you wish adding here a string but you can have whatever you like.
        public event SampleDelegate SampleEvent;

On your button press:
            // if we have an event handler assigned then fire it
            if (SampleEvent!= null) {
                SampleEvent(m_SampleParameter);
            }

In your form you want the event to be fired:
On constructing the custom control:
                            YourCustomControl.SampleEvent+= new YourCustomControl.SampleDelegate(ProcedureToBeCalledOnEventFired);

And finally the actual procedure:
        public void ProcedureToBeCalledOnEventFired(string SampleParameter) {
             // Whatever you want to do here.
        }

0
 
YiogiCommented:
You need an event delegate in your button listener control and have an event assigned from your frmLaunch that creates the control. I can post some code if you don't know how to do that.
0
 
npl77Author Commented:
Please do I have no idea how to do that.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
YiogiCommented:
Apologies a type there on the renaming:

On constructing the custom control:
                            YourCustomControlInstance.SampleEvent+= new YourCustomControl.SampleDelegate(ProcedureToBeCalledOnEventFired);

Basically you assign the event to an instance of the control.
0
 
YiogiCommented:
grr. Type above = typo. Yet another typo lol.
0
 
npl77Author Commented:
Thanks man, it worked perfectly!! Thanks for explaining it in the detail you did as well.
0
 
YiogiCommented:
Glad I could help.
0
All Courses

From novice to tech pro — start learning today.