Results 1 to 14 of 14

Thread: Issue with keydown Space/Enter

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    10

    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

  2. #2
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: Issue with keydown Space/Enter

    The keycode seems to be fine. The problem might be with the NextChest function.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    10

    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

  4. #4
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: Issue with keydown Space/Enter

    didAus please check your Form Property : "AcceptButton"

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    10

    Re: Issue with keydown Space/Enter

    Hi medsont, my property "AcceptButton" is set "(none)". What should it be ?
    thanks

  6. #6
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    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.....

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    10

    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

  8. #8
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    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:
    1. Private Sub ChestWindow_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.         Select Case e.KeyCode
    3.             Case Keys.NumPad1
    4.                 NextChest(e)
    5.         End Select
    6.     End Sub
    7.  
    8.     Private Sub NextChest(ByVal e As System.Windows.Forms.KeyEventArgs)
    9.         countParity += 1
    10.         PB_imgChest.Tag = "not draw"
    11.         Refresh()
    12.         If FirstWindow.CB_ViewNumber.Text = "1 View, No Feedback" Then
    13.             If (countImg = tabImg.Length - 1) Then 'End of the 50 images
    14.                 FirstWindow.StopTracking() 'Stop the tracking
    15.                 FirstWindow.ETdataToTxt() 'send data from datagridview to txt file
    16.  
    17.                 'because u r closing the form here......
    18.                 If e.KeyData = Keys.Enter OrElse e.KeyData = Keys.Space Then
    19.                     Return
    20.                 Else
    21.                     Me.Close() ' Close the window
    22.                 End If
    23.  
    24.  
    25.             End If
    26.             If (countImg < tabImg.Length - 1) Then 'less than 50 images
    27.                 countImg = countImg + 1 'increase the counter
    28.                 DisplayChest(countImg)
    29.                 LB_view.Text = 1 '1st view of this image
    30.             End If
    31.     End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    10

    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

  10. #10
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    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.....

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    10

    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!

  12. #12
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    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:
    1. Try
    2.             'your codes here
    3.         Catch ex As Exception
    4.             MessageBox.Show(ex.ToString, "Error: " & Err.Number, MessageBoxButtons.OK, MessageBoxIcon.Error)
    5.         End Try

  13. #13

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    10

    Re: Issue with keydown Space/Enter

    I am so sorry...it still does the same thing ...

  14. #14
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width