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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
YiogiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.