Adding keydown event handler

  • Thread starter Thread starter Young
  • Start date Start date
Y

Young

I create a sub that will add event handlers to text boxes.

The sub declaration look like this:

sub AddGotFocus(myTextBox as TextBox, GotFocusEvent as System.EventHandler)

AddHandler myTextBox.GotFoucs, GotFocusEvent

end sub

I call this sub using AddGotFocus(text1,addressof Gotfocus). This works
without any problem. When I try to do the same for the KeyDown and MouseDown
event, Eg.

sub AddKeyDown(myTextBox as TextBox, KeydownEvent as System.EventHandler)

AddHandler myTextBox.KeyDown, KeydownEvent
I get this error here: Value of type system.eventhandler cannot be
converted to system.windows.forms.keyeventhandler

end sub

sub AddMOuseDown(myTextBox as TextBox, MousedownEvent as
System.EventHandler)

AddHandler myTextBox.Mousedown, MousedownEvent
I get this error here: Value of type system.eventhandler cannot be
converted to system.windows.forms.keyeventhandler

end sub

Can someone please help?

TIA
Young
 
Young said:
sub AddKeyDown(myTextBox as TextBox, KeydownEvent as
System.EventHandler)
AddHandler myTextBox.KeyDown, KeydownEvent
I get this error here: Value of type system.eventhandler cannot be
converted to system.windows.forms.keyeventhandler

end sub

As the message says, you have to pass a KeyEventHandler.

If this is all the method does, I wonder why you need a method. You can
execute Addhandler in the caller instead of calling a method that executes
Addhandler.



Armin
 
Hi Armin,

Thanks for your reply.

How do I pass in a KeyEventHandler?

I created a sub and just pass it in like Address of SubName.

The sub that receives this is declared as MyEvent As System.EventHandler.
There's no System.KeyEventHandler.

I've a lot of forms so this is easier for me. Otherwise I've to do it
"locally" on each form. I also passed in other EventHandler - text got
focus, lost focus etc.

Young
 
Young said:
Hi Armin,

Thanks for your reply.

How do I pass in a KeyEventHandler?

I created a sub and just pass it in like Address of SubName.

The sub that receives this is declared as MyEvent As
System.EventHandler. There's no System.KeyEventHandler.

No, but there is KeyEventHandler. Full name is
System.Windows.Forms.KeyEventHandler.

(Using "symbol search" (Alt+F12 here) quickly finds it)

I've a lot of forms so this is easier for me. Otherwise I've to do it
"locally" on each form. I also passed in other EventHandler - text got
focus, lost focus etc.

Yes, but why is

AddGotFocus(text1,addressof Gotfocus)

better than

Addhandler text1.gotfocus, addressof Gotfocus

?

View chars less typing can't be the reason because you have to write the
additional AddGotFocus, AddkeyDown etc. subs.


Armin
 
It seems you have two choices.

You could change the declarations for the arguments from System.EventHandler
to System.Windows.Forms.KeyEventhandler, and adjust your event handling code
to process the different fields of the argument.

Or you could create a new event with the argument list you need
(System.Eventhandler) and raise that event in the corresponding event
handlers for the control (KeyDown and MouseDown in this example) and create
a common handler for this event instead of the control events.

The first option is simpler, but requires adding a handler for every event
you want to use. The second enables you to use a much smaller number of
handlers, but you need to add code for each control to raise the common
events.
 
Back
Top