Having my Event and Eating it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phill.  W

    Having my Event and Eating it?

    How do I propagate an Event that I've just handled?

    I have a UserControl containing, among other things, a TextBox.

    The UserControl handles KeyDown/KeyPress Events for both itself
    and the TextBox, as in

    Private Sub TextField_KeyDo wn( _
    ByVal sender As Object _
    , ByVal e As System.Windows. Forms.KeyEventA rgs _
    ) Handles MyBase.KeyDown, txtField.KeyDow n

    My problem is that I'd /also/ like the Outside World to be able to
    receive these Events as well. Should it be as simple as calling

    MyBase.OnKeyDow n()

    within the above Event Handler?
    Or is that going to give me a nasty loop, with the UserControl
    handling its own Event, over and over again?

    TIA,
    Phill W.


  • Ken Tucker [MVP]

    #2
    Re: Having my Event and Eating it?

    Hi,

    Here is some code from an user control that has a textbox. It has
    the user controls textchanged, and validating events fire when the textbox's
    events fire. Also makes the user controls text property change the
    textboxes text. Hope this helps.

    Public Shadows Event TextChanged(ByV al sender As Object, ByVal e As
    EventArgs)

    Public Shadows Event Validating(ByVa l sender As Object, ByRef e As
    System.Componen tModel.CancelEv entArgs)

    Public Shadows Property Text() As String

    Get

    Return TextBox1.Text

    End Get

    Set(ByVal Value As String)

    TextBox1.Text = Value

    End Set

    End Property

    Private Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles TextBox1.TextCh anged

    RaiseEvent TextChanged(Me, e)

    End Sub

    Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
    System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting

    RaiseEvent Validating(Me, e)

    End Sub



    Ken

    ---------------------

    "Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
    news:ciord0$nqf $1@yarrow.open. ac.uk...
    How do I propagate an Event that I've just handled?

    I have a UserControl containing, among other things, a TextBox.

    The UserControl handles KeyDown/KeyPress Events for both itself
    and the TextBox, as in

    Private Sub TextField_KeyDo wn( _
    ByVal sender As Object _
    , ByVal e As System.Windows. Forms.KeyEventA rgs _
    ) Handles MyBase.KeyDown, txtField.KeyDow n

    My problem is that I'd /also/ like the Outside World to be able to
    receive these Events as well. Should it be as simple as calling

    MyBase.OnKeyDow n()

    within the above Event Handler?
    Or is that going to give me a nasty loop, with the UserControl
    handling its own Event, over and over again?

    TIA,
    Phill W.



    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Having my Event and Eating it?

      Phill,
      Do you care who the sender is in the KeyDown event?

      I would override the MyBase.OnKeyDow n() to handle the user control's KeyDown
      event, being certain to call MyBase.OnKeyDow n so others see the event.

      Then in the txtField.KeyDow n event handler I would call MyClass.OnKeyDo wn.
      This handler would only handle the TextBox's event.

      Something like:

      Protected Overrides Sub OnKeyDown(ByVal e As
      System.Windows. Forms.KeyEventA rgs)
      ' do your thing here
      MyBase.OnKeyDow n(e)
      ' or/also do your thing here
      End Sub

      Private Sub TextField_KeyDo wn( _
      ByVal sender As Object _
      , ByVal e As System.Windows. Forms.KeyEventA rgs _
      ) Handles txtField.KeyDow n
      MyClass.OnKeyDo wn(e)
      End Sub

      If you cared who the sender is I would have the above two methods call an
      overloaded OnKeyDown method, which then calls MyBase.OnKeyDow n.

      Protected Overloads Sub OnKeyDown(ByVal sender As Object, ByVal e As
      System.Windows. Forms.KeyEventA rgs)
      ' do your thing here
      MyBase.OnKeyDow n(e)
      ' or/also do your thing here
      End Sub

      Protected Overrides Sub OnKeyDown(ByVal e As
      System.Windows. Forms.KeyEventA rgs)
      MyClass.OnKeyDo wn(Me, e)
      End Sub

      Private Sub TextField_KeyDo wn( _
      ByVal sender As Object _
      , ByVal e As System.Windows. Forms.KeyEventA rgs _
      ) Handles txtField.KeyDow n
      MyClass.OnKeyDo wn(sender, e)
      End Sub

      Looking at it you would not need OnKeyDown(sende r, KeyEventArgs) you could
      simply call TextField_KeyDo wn from OnKeyDown(KeyEv entArgs) then call
      MyBase.OnKeyDow n fromt here...

      Hope this helps
      Jay

      "Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
      news:ciord0$nqf $1@yarrow.open. ac.uk...[color=blue]
      > How do I propagate an Event that I've just handled?
      >
      > I have a UserControl containing, among other things, a TextBox.
      >
      > The UserControl handles KeyDown/KeyPress Events for both itself
      > and the TextBox, as in
      >
      > Private Sub TextField_KeyDo wn( _
      > ByVal sender As Object _
      > , ByVal e As System.Windows. Forms.KeyEventA rgs _
      > ) Handles MyBase.KeyDown, txtField.KeyDow n
      >
      > My problem is that I'd /also/ like the Outside World to be able to
      > receive these Events as well. Should it be as simple as calling
      >
      > MyBase.OnKeyDow n()
      >
      > within the above Event Handler?
      > Or is that going to give me a nasty loop, with the UserControl
      > handling its own Event, over and over again?
      >
      > TIA,
      > Phill W.
      >
      >[/color]


      Comment

      Working...