We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

how to addhandler for keydown event for datagridview combobox

codefinger
codefinger asked
on
Medium Priority
2,421 Views
Last Modified: 2012-05-11
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.
Comment
Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

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:
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
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.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

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  = )
Just checking. Wouldn't want to miss an opportunity to learn :)

Author

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.


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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.