Issue with keydown Space/Enter
Hi guys,
I am using vb.net within visual studio 2008.
I am using a picture box. I want to be able to use the Space or Enter key to change the image in it. For that, I try to use the keydown function as follow:
Code:
Private Sub ChestWindow_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad1
NextChest()
Case Keys.Enter
NextChest()
Case Keys.Space
NextChest()
End Select
End Sub
NextChest() is the function which changes the image. It works well when I press '1' on my numeric part of the keyboard as a test.
The issue is that when I press Space or Enter, the form containing the picturebox closes. (I've already put the keyPreview property to True)
I really do not understand. Do these keys have any default behavior? How can I do to make them doing what I want?
Thanks for your help!
Did
Re: Issue with keydown Space/Enter
The keycode seems to be fine. The problem might be with the NextChest function.
Re: Issue with keydown Space/Enter
Hi Ram2Curious,
Thanks for your very fast answer.
I am nearly sure that the issue is here. I've been using this function for a while, and I never had any problem with it.
another strange thing is that when the function is written as follow, the same thing happens with space and enter keys ...
Code:
Private Sub ChestWindow_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad1
NextChest()
End Select
End Sub
Thanks
Did
Re: Issue with keydown Space/Enter
didAus please check your Form Property : "AcceptButton"
Re: Issue with keydown Space/Enter
Hi medsont, my property "AcceptButton" is set "(none)". What should it be ?
thanks
Re: Issue with keydown Space/Enter
Accept button
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx
please post your Nextchest function here.....
Re: Issue with keydown Space/Enter
Here is my NextChest() function:
Code:
Private Sub NextChest()
countParity += 1
PB_imgChest.Tag = "not draw"
Refresh()
If FirstWindow.CB_ViewNumber.Text = "1 View, No Feedback" Then
If (countImg = tabImg.Length - 1) Then 'End of the 50 images
FirstWindow.StopTracking() 'Stop the tracking
FirstWindow.ETdataToTxt() 'send data from datagridview to txt file
Me.Close() ' Close the window
End If
If (countImg < tabImg.Length - 1) Then 'less than 50 images
countImg = countImg + 1 'increase the counter
DisplayChest(countImg)
LB_view.Text = 1 '1st view of this image
End If
The counter countImg is set to 0 at the loading of the form. I am sure that it's not equal to tabImg.Length-1 when I have the issue.
Thanks guys
Did
Re: Issue with keydown Space/Enter
Hai DidAus... please try this below code if it will work i'm very happy otherwise VBFORUMS has more superior programmers so someone is might help you...........
vb Code:
Private Sub ChestWindow_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad1
NextChest(e)
End Select
End Sub
Private Sub NextChest(ByVal e As System.Windows.Forms.KeyEventArgs)
countParity += 1
PB_imgChest.Tag = "not draw"
Refresh()
If FirstWindow.CB_ViewNumber.Text = "1 View, No Feedback" Then
If (countImg = tabImg.Length - 1) Then 'End of the 50 images
FirstWindow.StopTracking() 'Stop the tracking
FirstWindow.ETdataToTxt() 'send data from datagridview to txt file
'because u r closing the form here......
If e.KeyData = Keys.Enter OrElse e.KeyData = Keys.Space Then
Return
Else
Me.Close() ' Close the window
End If
End If
If (countImg < tabImg.Length - 1) Then 'less than 50 images
countImg = countImg + 1 'increase the counter
DisplayChest(countImg)
LB_view.Text = 1 '1st view of this image
End If
End Sub
Re: Issue with keydown Space/Enter
Thanks for your suggestion. I will try that and let you know how it works !
Can you explain me what you did exactly ? I'm not sure I understand ...
Did
Re: Issue with keydown Space/Enter
hey please, first post it's working or not.......... i just stopped to close the form ["Me.Close()]
if current keycode is Enter or Space thats all.....
Re: Issue with keydown Space/Enter
Hi medsont, I just tried it, and unfortunately it doesn't works. The same thing is still happening...
But thanks for your help, I really appreciate!
Re: Issue with keydown Space/Enter
it's k .....if you can not find the exact error write your code with in Try...Catch block..........
vb Code:
Try
'your codes here
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error: " & Err.Number, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Re: Issue with keydown Space/Enter
I am so sorry...it still does the same thing ...
Re: Issue with keydown Space/Enter
Is the AcceptButton property set on the Picturebox Forms? If so try removing the value. Other than that I agree with medsont, the KeyDown event looks fine (although it is fired by the Forms Keydown event. Is that right? Or is meant to fire on the Keydown event of the Picturebox?)
If none of the above things are the problem perhaps you could post the project files for a deeper look into the problem. As it may be that there are other events in your project that are causing the behaviour.