|
-
Dec 6th, 2008, 03:24 AM
#1
Thread Starter
Member
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!
-
Dec 6th, 2008, 06:06 AM
#2
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?
-
Dec 6th, 2008, 06:43 AM
#3
-
Dec 6th, 2008, 06:48 AM
#4
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.
-
Dec 6th, 2008, 07:48 AM
#5
Thread Starter
Member
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.
-
Dec 6th, 2008, 07:50 AM
#6
Thread Starter
Member
Re: Capturing a keyboard input
oh ya! Just to add on keystone_paul you have got what i meant.
-
Dec 6th, 2008, 08:21 AM
#7
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:
Private Sub dgvMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvMain.KeyDown If m_bVoidMode Then If e.KeyCode = Keys.Enter Then 'do deleting work here End If End If 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|