PC Review


Reply
Thread Tools Rate Thread

Adding keydown event handler

 
 
Young
Guest
Posts: n/a
 
      4th Jun 2009
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


 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      4th Jun 2009
Young wrote:
> 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

 
Reply With Quote
 
 
 
 
Young
Guest
Posts: n/a
 
      5th Jun 2009
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

"Armin Zingler" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Young wrote:
>> 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



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      5th Jun 2009

Young wrote:
> 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

 
Reply With Quote
 
James Hahn
Guest
Posts: n/a
 
      5th Jun 2009
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.

"Young" <(E-Mail Removed)> wrote in message
news:4a278b51$(E-Mail Removed)...
>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
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it any point to make these two event handler into one event handler Tony Johansson Microsoft C# .NET 1 22nd Dec 2012 01:02 AM
RE: KeyDown Event Handler Generator Code Disappears Steve Behman Microsoft VC .NET 0 15th Aug 2008 12:34 AM
Re: KeyDown Event Handler Generator Code Disappears Pavel Minaev Microsoft VC .NET 0 14th Aug 2008 07:48 AM
Event Handler that creates adds another event handler kaczmar2@gmail.com Microsoft ASP .NET 1 22nd Feb 2007 08:37 AM
KeyUp + KeyDown Event Handler ShaneO Microsoft VB .NET 4 7th Feb 2006 12:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:36 PM.