[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ignoring a KeyDown Event on a DataGridView

Posted on 2007-10-04
8
Medium Priority
?
437 Views
Last Modified: 2008-01-09
Hi all,

I have the following sub on a form of my project:

Private Sub lnklblSupplier_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnklblSupplier.LinkClicked
        If frmLookupSuppliers.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtboxSuppCode.Text = frmLookupSuppliers.dgvLookup.SelectedRows(0).Cells(0).Value
        End If
    End Sub

When the linklabel is clicked it opens another form with a populated DataGridView on it. I have set on the DGV form that when a user double clicks the DGV it passes the value from the clicked row back to the main form and this works fine. However, I also want the user to be able to press the Return key and have the selected value be returned to the main form.
The problem I am having is that when the user presses return and value that is being passed back is the following row from the DGV because the return keypress is moving the selection down one. Is there any way I can stop the return keypress moving the selection of the DGV?

Thanks
0
Comment
Question by:kiranboi
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:Jai S
ID: 20012721
http://www.developerfood.com/datagridview-use-enter-key-instead-of-the-tab-key/microsoft-public-dotnet-languages-vb/2863ea63-22e4-4eff-a4a6-a567b94b681e/article.aspx

it's right to override the ProcessDialogKey method in the DataGridView
subclass. ProcessDialogKey method is called in edit mode to handle keyboard
input that is not handled by the hosted editing control.

To sum up:
When the DataGridView is not in edit mode, we could handle the KeyPress
event of the DataGridView to capture key strokes.


We could also override the ProcessDataGridViewKey method in the
DataGridView subclass to get notified before the key stroke is sent to the
DataGridView.


When the DataGridView is in edit mode, we could handle the
EditingControlShowing event to get the hosted editing control in the
current cell and then subscribe the KeyPress event of the hosted editing
control in turn.


We could also override the ProcessDialogKey method in the DataGridView
subclass to get notified before the key stroke is sent to the DataGridView.
0
 

Author Comment

by:kiranboi
ID: 20012750
OK now youve confused me :op

Ive had a look at the link you sent and inserted the following into my code:

    Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        Dim returnValue As Boolean
        returnValue = Me.ProcessDialogKey(keyData)
    End Function

but am still having the same trouble. what am I doing wrong?
0
 
LVL 14

Expert Comment

by:Jai S
ID: 20012759
just check whether enterkey is pressed ...and return false...

Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        Dim returnValue As Boolean
        returnValue = Me.ProcessDialogKey(keyData)
'check the keydata for enter key and return false...
retutn false
    End Function
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:kiranboi
ID: 20012767
Ive tried that but when watching the function with a break point it never gets called?
0
 

Author Comment

by:kiranboi
ID: 20012791
Sorted it, I just changed my sub to the following instead. I forgot all about the SuppressKeyPress Property

Private Sub dgvLookup_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvLookup.KeyDown
        If e.KeyCode = Keys.Return Then
            e.SuppressKeyPress = True
            acceptSelection()
        End If
    End Sub
0
 
LVL 14

Expert Comment

by:Jai S
ID: 20012799
jst see whether your DGV works is EDIT mode - when you change some value and and press the enter kley the that value has to be commited...if it works then its great....
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 20237769
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question