keydown event doesn't fire after button click

T

Terry Brown

I am building a stopwatch which I want to start and stop by either
pressing a navigation button or an on-screen button. I update the timer
display by using a Timer with the interval set to 100 milliseconds

My code looks like this:

Private Sub Clock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock.Tick
If clock_enabled Then
QueryPerformanceCounter(ticker)
ClockButton.Text = MakeTimeString(ticker - startTicker)
End If
End Sub

Private Sub ClockButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClockButton.Click
If clock_enabled Then
clock_enabled = False
Clock.Enabled = False
Else
QueryPerformanceCounter(Form1.startTicker)
clock_enabled = True
Clock.Enabled = True
End If
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim str As String
'any of the navigation keys will start/stop the clock
If e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Or e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then
If clock_enabled Then
clock_enabled = False
Clock.Enabled = False
Else
QueryPerformanceCounter(Form1.startTicker)
clock_enabled = True
Clock.Enabled = True
End If
End If
End Sub

The ClockButton_Click routine always works and behaves as I expect.

The Form1_KeyDown routine also works, but only if I don't cause
ClockButton_Click to be activated. I can press any of the navigation
buttons and the clock starts and stops as expected, but if I press the
ClockButton (the onscreen button) which causes ClockButton_Click to be
executed, then from that point on, Form1_KeyDown does not get executed
when I press a navigation button.

The reason I duplicate clock_enabled and Clock.Enabled is for ease of
information passing to other classes.

Anyone have an idea about what might be going on here??

thanks,

Terry Brown
Stickman Software
http://www.stickmansoftware.com
 
G

Guest

Not sure how accurate you need your stopwatch to be, but the Timer in all
Windows (desktop or CE) is highly inaccurate. It's guaranteed to never be
short, but is very often long, and often by a significant amount.

-Chris
 
T

Terry Brown

Yes, but note that I don't use the Timer to actually time anything--it is
just used to periodically update the display. The timing is done with
QueryPerformanceCounter and QueryPerformanceFrequency.

Any ideas on the problem with button event causing the keydown event not
to fire??

Thanks,

Terry Brown
Stickman Software
http://www.stickmansoftware.com
 
T

Torbjorn Stavas

Try to set the focus to the form after the button has been pressed,
i.e Form1.Focus().

This usually solves it.

//torbjörn
 
C

Chris Tacke, eMVP

The keydown is going to the control with focus. When you click on a button,
focus changes from the Form to the control, so the control itself will get
the event, but not the parent Form.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 

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