|
-
Mar 7th, 2009, 12:07 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2008] KeyDown and KeyPress events not responding
In the application I'm working on I use F1 and also a right mouse click to close the application since the graphics display full screen and there's no control in the upper right corner to close with. I use a good number of different keypresses to control things and everything works fine. When I switch away from the form that the applications starts in to a different form I'm not getting any response from either a KeyDown or a KeyPress event. Maybe someone knows what's going on. The ButtonClick and MouseDown events are responding just fine but for some reason the KeyDown and KeyPress events aren't. Below is code for one of the Forms that is unresponsive in this way. I put Close() outside the If Thens below so that any
KeyPress or KeyDown would close but nothing happens.
Code:
Option Strict On
Public Class InstructionsForRayRover
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TellAboutwOn = 0
StartForm1 = 1
mainForm = New DisplayGraphics
Me.Close()
End Sub
Private Sub InstructionsForRayRover_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If mainForm Is Me Then
mainForm = Nothing
End If
End Sub
Private Sub InstructionsForRayRover_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Close()
If e.KeyCode = Keys.F1 Then
End If
End Sub
Private Sub InstructionsForRayRover_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Close()
If e.KeyChar = "a"c Then
End If
End Sub
Private Sub InstructionsForRayRover_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This is just to center the text at the top of the form. There might be a more efficient way to to this
st2 = My.Computer.Info.OSFullName
b = st2.IndexOf("XP") ' b will = -1 if any version of Windows XP isn't being used
If b >= 0 Then
a = CInt((Hxlimit - 96) / 4.07)
Else
a = CInt((Hxlimit - 96) / 3)
End If
st1 = ""
For i = 0 To a
st1 = st1 & " "
Next
Me.Text = st1 & "Instructions On Using Ray Rover"
Label1.Left = HxlimitI - 364
Label3.Left = HxlimitI - 364
Button1.Left = HxlimitI - 60
ButtonUpdate.Left = HxlimitI - 106
Label2.Width = XlimitI - 25
End Sub
Private Sub InstructionsForRayRover_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
If MouseButtons = MouseButtons.Right Then
Close()
End If
End Sub
End Class
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Mar 7th, 2009, 12:39 PM
#2
Re: [2008] KeyDown and KeyPress events not responding
Set the form KeyPreview property to true at design time or in your Form_Load event.
-
Mar 7th, 2009, 05:23 PM
#3
Thread Starter
Fanatic Member
Re: [2008] KeyDown and KeyPress events not responding
Thanks very much, that took care of the problem. What's interesting is that KeyPreview for my starting form was set to false but it does respond to KeyPress events and my Form_Load event doesn't specify anything about KeyPreview. Anyway problem fixed.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Mar 7th, 2009, 05:26 PM
#4
Re: [2008] KeyDown and KeyPress events not responding
 Originally Posted by EntityX
Thanks very much, that took care of the problem. What's interesting is that KeyPreview for my starting form was set to false but it does respond to KeyPress events and my Form_Load event doesn't specify anything about KeyPreview. Anyway problem fixed.
That's because as long as the focus is on your form, you don't need KeyPreview on. If you have anything that draws immediate focus, such as a Button or TextBox, then the form won't be getting it. But if your first form was just labels and such, you wouldn't need the preview on.
-
Mar 7th, 2009, 06:16 PM
#5
Thread Starter
Fanatic Member
Re: [RESOLVED] [2008] KeyDown and KeyPress events not responding
That explains it. My starting form doesn't have any textbox or similar objects, just labels.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|