Delegates and EventHandlers

G

Guest

I need some assistance doing some "right way to do it" coding. The following
are EventHandlers associated with Delegates in a child form that call a
procedure in the MDI form that resets a timer. There must be a better way to
code this than calling OnResetTimer in all these procedures separately. Is
there not a way to use one handler for all of them?

private void pnlCPI_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
OnResetTimer(this);
}

private void txtCPI_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
OnResetTimer(this);
}

private void dgLevelOfCare_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
OnResetTimer(this);
}

private void dgLevelOfCare_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
OnResetTimer(this);
}
 
A

Alexander Shirshov

Robert,

Is OnResetTimer your own method? Change it's signature to be:

private void OnResetTimer(object sender, EventArgs e)

then in Form's designer property page switch to Events view (an icon with a
thunderbolt). Now select the control you want and OnResetTimer should appear
in a dropdown list and you can assign it to events you want.

Alternatively, just FYI, you could override WndProc and reset timer there
(sorry, don't have examples right now).

HTH,
Alexander
 
G

Guest

OnResetTimer is my owm method in a MDI form. It is being called from a child
form of the MDI firm using Delegates and EventHandlers. The deal is that the
user has to log back into the application if there is no activity in 30
minutes. I have a timer on the MDI form that gets reset everytime there is
activity such as MouseMove or KeyPress on a child form. I have no EventArgs
to pass back to the OnResetTimer method so I am just passing back the sender.
I am just looking for a more elegant way to handle the call to OnResetTimer.

Thanks,

Robert

Alexander Shirshov said:
Robert,

Is OnResetTimer your own method? Change it's signature to be:

private void OnResetTimer(object sender, EventArgs e)

then in Form's designer property page switch to Events view (an icon with a
thunderbolt). Now select the control you want and OnResetTimer should appear
in a dropdown list and you can assign it to events you want.

Alternatively, just FYI, you could override WndProc and reset timer there
(sorry, don't have examples right now).

HTH,
Alexander
 
A

Alexander Shirshov

Here's the version with overriding WndProc if for some reason you didn't
like my previous solution:

private const int WM_KEYFIRST = 0x0100;
private const int WM_KEYLAST = 0x0108;

private const int WM_MOUSEFIRST = 0x0200;
private const int WM_MOUSELAST = 0x020A;

protected override void WndProc(ref Message m)
{
if (m.Msg >= WM_KEYFIRST & m.Msg <= WM_KEYLAST)
{
OnResetTimer(this);
}

if (m.Msg >= WM_MOUSEFIRST & m.Msg <= WM_MOUSELAST)
{
OnResetTimer(this);
}

base.WndProc (ref m);
}

Robert said:
OnResetTimer is my owm method in a MDI form. It is being called from a
child
form of the MDI firm using Delegates and EventHandlers. The deal is that
the
user has to log back into the application if there is no activity in 30
minutes. I have a timer on the MDI form that gets reset everytime there
is
activity such as MouseMove or KeyPress on a child form. I have no
EventArgs
to pass back to the OnResetTimer method so I am just passing back the
sender.
I am just looking for a more elegant way to handle the call to
OnResetTimer.

Thanks,

Robert
 

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