• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2121
  • Last Modified:

how to addhandler for keydown event for datagridview combobox

Trying to create an event handler for the combobox  (cbdev) keydown event in my datagrid, this does not work:

AddHandler cbdev.KeyDown, New EventHandler(AddressOf cbdev_KeyDown)

Private Sub cbdev_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    ....do something
End Sub

ERROR MESSAGE:
Nested sub does not have a signature that is compatible with delegate 'Delegate Sub KeyEventHandler(sender As Object, e As System.Windows.Forms.KeyEventArgs)'.

I have tried various combinations, but cannot find an approach that works.

Please advise.

Thanks in advance.
0
codefinger
Asked:
codefinger
  • 5
  • 3
3 Solutions
 
Jeff CertainCommented:
AddHandler cbdev.KeyDown, AddressOf cbdev_KeyDown
0
 
Jeff CertainCommented:
A simpler approach is:

Private Sub cbdev_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbdev.KeyDown
    ....do something
End Sub
0
 
käµfm³d 👽Commented:
Chaosian's alternatives are right on the money, however, the problem is your trying to pass a function with a signature of:

   
ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs

Open in new window


but EventHandler expects

   
ByVal sender As Object, ByVal e As System.EventHandler

Open in new window


You could try using this instead:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
Arrggghh....   I sear I had that code in there. Here it is:
AddHandler cbdev.KeyDown, New KeyEventHandler(AddressOf cbdev_KeyDown)

Open in new window

0
 
Jeff CertainCommented:
kaufmed,

Any reason to explicitly create it as a KeyEventHandler instead of letting the AddHandler method sort it all out? Just curious if there's something I'm missing here.
0
 
käµfm³d 👽Commented:
Any reason to explicitly create it as a KeyEventHandler instead of letting the AddHandler method sort it all out?
No. I agreed with your initial assessment. I was just demonstrating how the original code *could* be corrected. I would still go with your offerings as a primary resort  = )
0
 
Jeff CertainCommented:
Just checking. Wouldn't want to miss an opportunity to learn :)
0
 
codefingerAuthor Commented:
Chaosian:

Private Sub cbdev_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbdev.KeyDown
    ....do something
End Sub

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.


0
 
Jeff CertainCommented:
Sounds like you're creating cbdev yourself, rather than letting the designer declare it for you. (I should have understood this from the fact that you mentioned the control was in a grid.)

You'll need to use AddHandler in this case.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now