Results 1 to 7 of 7

Thread: Capturing a keyboard input

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    63

    Capturing a keyboard input

    Hello! I would like to know how do i capture the key input by a user on a keyboard?


    Private Sub btnVoid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVoid.Click
    Dim rowIndex As Integer = DataGridView1.CurrentRow.Index

    'if user press the enter key then
    'Remove the the row from the datagridview1 represented by the rowIndex.


    End Sub

    Help is greatly appreciated.

    Thanks!

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Capturing a keyboard input

    I'm a bit confused by the code you've posted.


    You wouldn't capture keyboard input in a button click event.

    Are you trying to detect if the user presses the enter key regardless of which control on the form has focus?

  3. #3
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: Capturing a keyboard input

    Set the 'Keypreview' setting of the form to 'True' and then use the following code in the keydown event of the form.

    Code:
    If e.KeyCode=Keys.Enter Then
    ''Do stuff here if the user pressed enter
    End If
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Capturing a keyboard input

    The only problem with the keypreview approach is it doesn't seem to work with the enter key when there are other controls on the form.

    Other keys work fine, but not the enter key.

    It may be simplest - if I'm understanding what teocl5 is trying to do - is to have a button on the form and set it to be the form's accept button and put the code in that button's click event. That way it will get executed if the user hits enter.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    63

    Re: Capturing a keyboard input

    What i wanted to do was to delete data from datagridview. Upon clicking on the "void" button the page will start listening for user input. If user press the "Enter" key the page will delete the row which has the focus.

    I hope i am clear and sorry for being unclear in the first place.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    63

    Re: Capturing a keyboard input

    oh ya! Just to add on keystone_paul you have got what i meant.

  7. #7
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Capturing a keyboard input

    OK I understand, although its an unusual way of doing things (whats wrong with just a delete button that also gets fired when the user hits enter?). Presumably there is another button somewhere to tell the program to stop deleting rows on hitting the enter key?

    Structure wise you wouldn't put your "listening" code in the button click event.

    How I would do it is to create a form-level boolean variable m_bVoidMode and set this to true when the user clicks the Void button.

    Then in your datagridview keydown event put the code to test that the user is in "voiding" mode and test for the enter key and then do your deletion.

    vb Code:
    1. Private Sub dgvMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvMain.KeyDown
    2.  
    3.         If m_bVoidMode Then
    4.             If e.KeyCode = Keys.Enter Then
    5.                 'do deleting work here
    6.             End If
    7.         End If
    8.  
    9.     End Sub
    Last edited by keystone_paul; Dec 6th, 2008 at 08:38 AM.

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