Adding keydown event handler

Discussion in 'Microsoft VB .NET' started by Young, Jun 4, 2009.

  1. Young

    Young Guest

    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, Jun 4, 2009
    #1
    1. Advertisements

  2. 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
     
    Armin Zingler, Jun 4, 2009
    #2
    1. Advertisements

  3. Young

    Young Guest

    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" <> wrote in message
    news:%...
    > 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
     
    Young, Jun 5, 2009
    #3
  4. 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
     
    Armin Zingler, Jun 5, 2009
    #4
  5. Young

    James Hahn Guest

    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" <> wrote in message
    news:4a278b51$...
    >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
    >
     
    James Hahn, Jun 5, 2009
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Law
    Replies:
    4
    Views:
    1,630
    Herfried K. Wagner [MVP]
    Jun 10, 2004
  2. VJ

    Remove Handler.. & Add Handler...

    VJ, Jun 11, 2004, in forum: Microsoft VB .NET
    Replies:
    5
    Views:
    441
  3. Just Me

    Adding an event handler in a sub

    Just Me, Aug 9, 2004, in forum: Microsoft VB .NET
    Replies:
    7
    Views:
    175
    Just Me
    Aug 10, 2004
  4. Just Me

    Adding an event handler in a sub

    Just Me, Aug 9, 2004, in forum: Microsoft VB .NET
    Replies:
    6
    Views:
    202
    Craig Vick [MSFT]
    Sep 3, 2004
  5. ShaneO

    KeyUp + KeyDown Event Handler

    ShaneO, Feb 6, 2006, in forum: Microsoft VB .NET
    Replies:
    4
    Views:
    569
    ShaneO
    Feb 6, 2006
Loading...

Share This Page