Intercepting events

J

joe

Hello all,

I have a form with several controls(buttons) on it. I want
to start a screen saver after nothing has been pressed for
10 min. And of course if something has been pressed, to
restart the timer. To to this I have to trap each
control's Click event and restart the timer from there. Is
there a way to do this only once, somehow to hook before
each control's Click event or something sismilar?
Is there a way the form to intercept an event going one of
its controls?

Thank you
 
B

Brian

Hey.

How about...

create another event handler:

private void anyButton_Click( object sender, System.EventArgs e )
{
tmrStartScreenSaver.Enabled = false;
tmrStartScreenSaver.Enabled = true;
}

To save typing connecting up your events:

public Form1()
{
InitializeComponents();

foreach( Control control in this.Controls )
if( control is Button )
control.Click += new EventHandler( this.anyButton_Click );
}

If this makes sense, you're probably using C#. If your using VB.NET it will
be more of a problem.

Brian.
 
B

Brian

Because in C# you add the event handlers manually...
mycontrol.someEvent += new EventHandler( this.myEventHandler )

in VB.net... oh, wait I just looked it up, there's actually 2 ways in VB.

If you were connecting your events using Handles you'd have to type them
manually but you can do the same as C# by using AddHandler
(I learnt something new today and it's only 10:00 on a Sunday).

I'm not sure how things will work if Handles is used for the normal event
and AddHandler for the reset timer event, but it's easy enough to test.

There goes one of the reasons I prefer C#. I think I'll just pretend it
never happened.

Brian.
 
H

Herfried K. Wagner

Hello,

Brian said:
Because in C# you add the event handlers manually...
mycontrol.someEvent += new EventHandler( this.myEventHandler )

in VB.net... oh, wait I just looked it up, there's actually 2 ways
in VB.
;-)

If you were connecting your events using Handles you'd
have to type them manually but you can do the same as
C# by using AddHandler (I learnt something new today and
it's only 10:00 on a Sunday).

I'm not sure how things will work if Handles is used for the
normal event and AddHandler for the reset timer event, but it's
easy enough to test.

You can disable a "Handles" event handler by calling RemoveHandler and
re-add it by calling AddHandler.

Regards,
Herfried K. Wagner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top