CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2011
    Posts
    16

    VS 2008: 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:

    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
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: VS 2008: Issue with keydown Space/Enter

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VS 2008: Issue with keydown Space/Enter

    Space or enter may activate the selected button in your app.

    You should add code to your forms keypreview event call your function and set e.handled=true to keep the button from being processed in another manner.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Aug 2011
    Posts
    16

    Re: VS 2008: Issue with keydown Space/Enter

    Hi guys,

    thanks dglienna, but I already saw that before.

    DataMiser, I don't totally understand what you propose me to try ?
    What do you mean with "You should add code to your forms keypreview event call your function" ?
    I am sorry for that, my native language is not english so sometimes it's tough for me to follow this kind of sentences. I don't have anything such as KeyPreview, except the window property, which I already set to True.

    Thanks

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VS 2008: Issue with keydown Space/Enter

    Sorry, When key preview is on the Form_KeyDown and Form_KeyPress events fire first to give you a preview of the key hit by the user. It is here that you need your code if that is not what you have already. If your form is named ChestWindow then your code is in the right place, if not then it needs to be moved.

    In either case you need to add e.handled=true to each case to stop the key event from being passed on.

    Code:
    Case Keys.NumPad1
       NextChest()
       e.Handled=true
    Case Keys.Enter
       NextChest()
       e.Handled=true
    Case Keys.Space
       NextChest()
       e.Handled=true
    Always use [code][/code] tags when posting code.

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

Featured