Event question

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

    Event question

    I am just getting started with events and had a couple of questions on why
    they do what they do.

    If you have a textbox and you want to handle an event you can just do:

    this.TextBox2.T extChanged += new
    EventHandler(Te xtBox2_TextChan ged);
    and then have the function.

    void TextBox2_TextCh anged(object sender, EventArgs e)
    {
    Response.Write( this.TextBox2.T ext);
    }

    Here the event is created and tied to the control and function.

    But if you do it in a user control, the function doesn't seem to be tied to
    anything.

    For example, in my .ascx page I could have this:
    public event EventHandler BubbleTextChang ed;
    //protected void OnBubbleTextCha nged(EventArgs e)
    //{
    // //if (BubbleTextChan ged != null)
    // //BubbleTextChang ed(this, e);
    //}

    Where I normally wouldn't comment out the function, so it isn't tied to
    anything (or doesn't seem to be). And the function seems to always need the
    word "on" in front of it.

    If I had 3 events (and no functions) they would show up in intellisense with
    "on" in front of them:

    public event EventHandler A1;

    public event EventHandler B1;

    public event EventHandler C1;

    public event EventHandler D1;

    public event EventHandler E1;

    If I only had the above and nothing more, in my aspx page my intellisense
    would show:

    onA1
    onB1
    onC1
    onD1
    onE1

    Yet in my first example, the event was TextBox2.TextCh anged no
    TextBox2.onText Changed or the event that was tied to that:
    TextBox2_TextCh anged wasn't onTextBox2_Text Changed.

    Why is it different?

    Thanks,

    Tom


  • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

    #2
    RE: Event question

    events are pretty simple.

    an event is just a collection of delegates (functions to call).
    if you have an object that wants to raise events, that object defines a
    public event (any name you want). when the object wants to raise events it
    calls the delegates stored in its event object. the event will be null if no
    delegates have been added. by convention, the routine that raises the event
    is called OnEventName, and is code like:

    public EventHandler MyEvent;
    private void OnRaiseMyEvent( EventArgs e)
    {
    EventHandler handler = MyEvent;
    if (handler != null)
    handler(this, e);
    }

    some point in your code when you want to fire the event, it would call
    OnRaiseMyEvent.

    to subscribe to an event, your code addes a delegate (with the correct
    signature) to the event, via the syntax of the language. in c# its one of the
    following:

    myObject.MyEven t += new EventHandler(my Function);
    myObject.MyEven t += delegate(object o, EventArgs e)
    {
    // code goes here
    }
    myObject.MyEven t += (o,e) =>
    {
    // code goes here
    }


    -- bruce (sqlwork.com)


    "tshad" wrote:
    I am just getting started with events and had a couple of questions on why
    they do what they do.
    >
    If you have a textbox and you want to handle an event you can just do:
    >
    this.TextBox2.T extChanged += new
    EventHandler(Te xtBox2_TextChan ged);
    and then have the function.
    >
    void TextBox2_TextCh anged(object sender, EventArgs e)
    {
    Response.Write( this.TextBox2.T ext);
    }
    >
    Here the event is created and tied to the control and function.
    >
    But if you do it in a user control, the function doesn't seem to be tied to
    anything.
    >
    For example, in my .ascx page I could have this:
    public event EventHandler BubbleTextChang ed;
    //protected void OnBubbleTextCha nged(EventArgs e)
    //{
    // //if (BubbleTextChan ged != null)
    // //BubbleTextChang ed(this, e);
    //}
    >
    Where I normally wouldn't comment out the function, so it isn't tied to
    anything (or doesn't seem to be). And the function seems to always need the
    word "on" in front of it.
    >
    If I had 3 events (and no functions) they would show up in intellisense with
    "on" in front of them:
    >
    public event EventHandler A1;
    >
    public event EventHandler B1;
    >
    public event EventHandler C1;
    >
    public event EventHandler D1;
    >
    public event EventHandler E1;
    >
    If I only had the above and nothing more, in my aspx page my intellisense
    would show:
    >
    onA1
    onB1
    onC1
    onD1
    onE1
    >
    Yet in my first example, the event was TextBox2.TextCh anged no
    TextBox2.onText Changed or the event that was tied to that:
    TextBox2_TextCh anged wasn't onTextBox2_Text Changed.
    >
    Why is it different?
    >
    Thanks,
    >
    Tom
    >
    >
    >

    Comment

    • tshad

      #3
      Re: Event question


      "bruce barker" <brucebarker@di scussions.micro soft.comwrote in message
      news:CAB6BA8B-925F-45B5-9336-F6F556FBBEF0@mi crosoft.com...
      events are pretty simple.
      >
      an event is just a collection of delegates (functions to call).
      if you have an object that wants to raise events, that object defines a
      public event (any name you want). when the object wants to raise events it
      calls the delegates stored in its event object. the event will be null if
      no
      delegates have been added. by convention, the routine that raises the
      event
      is called OnEventName, and is code like:
      >
      public EventHandler MyEvent;
      private void OnRaiseMyEvent( EventArgs e)
      {
      EventHandler handler = MyEvent;
      if (handler != null)
      handler(this, e);
      }
      >
      But in your example you use something different. How does MyEvent know it
      is tied to OnRaiseMyEvent? The only place I can see that is in the
      OnRaiseMyEvent function:

      EventHandler handler = MyEvent;

      Whe is that executed to connect the two?

      In my code:

      public event EventHandler A1;
      public event EventHandler B1;
      public event EventHandler C1;

      Even with out setting up the onA1, onB1 and onC1 - the system seemed to use
      those names as defaults since in my other page that used the control,
      intellisense was using them.

      In you example, you use OnRaiseMyEvent and not OnMyEvent. Does it matter?
      You said it was a convention.

      I am just trying to understand it here. I don't see anywhere where it says
      something like: MyEvent uses OnRaiseMyEvent.
      some point in your code when you want to fire the event, it would call
      OnRaiseMyEvent.
      In the case of a user control, I assume it would be on the User control on
      the aspx page (or another ascx page).
      >
      to subscribe to an event, your code addes a delegate (with the correct
      signature) to the event, via the syntax of the language. in c# its one of
      the
      following:
      >
      myObject.MyEven t += new EventHandler(my Function);
      myObject.MyEven t += delegate(object o, EventArgs e)
      Do you use both of these? or just one?

      Thanks,

      Tom
      {
      // code goes here
      }
      myObject.MyEven t += (o,e) =>
      {
      // code goes here
      }
      >
      >
      -- bruce (sqlwork.com)
      >
      >
      "tshad" wrote:
      >
      >I am just getting started with events and had a couple of questions on
      >why
      >they do what they do.
      >>
      >If you have a textbox and you want to handle an event you can just do:
      >>
      > this.TextBox2.T extChanged += new
      >EventHandler(T extBox2_TextCha nged);
      >and then have the function.
      >>
      > void TextBox2_TextCh anged(object sender, EventArgs e)
      > {
      > Response.Write( this.TextBox2.T ext);
      > }
      >>
      >Here the event is created and tied to the control and function.
      >>
      >But if you do it in a user control, the function doesn't seem to be tied
      >to
      >anything.
      >>
      >For example, in my .ascx page I could have this:
      > public event EventHandler BubbleTextChang ed;
      > //protected void OnBubbleTextCha nged(EventArgs e)
      > //{
      > // //if (BubbleTextChan ged != null)
      > // //BubbleTextChang ed(this, e);
      > //}
      >>
      >Where I normally wouldn't comment out the function, so it isn't tied to
      >anything (or doesn't seem to be). And the function seems to always need
      >the
      >word "on" in front of it.
      >>
      >If I had 3 events (and no functions) they would show up in intellisense
      >with
      >"on" in front of them:
      >>
      > public event EventHandler A1;
      >>
      > public event EventHandler B1;
      >>
      > public event EventHandler C1;
      >>
      > public event EventHandler D1;
      >>
      > public event EventHandler E1;
      >>
      >If I only had the above and nothing more, in my aspx page my intellisense
      >would show:
      >>
      >onA1
      >onB1
      >onC1
      >onD1
      >onE1
      >>
      >Yet in my first example, the event was TextBox2.TextCh anged no
      >TextBox2.onTex tChanged or the event that was tied to that:
      >TextBox2_TextC hanged wasn't onTextBox2_Text Changed.
      >>
      >Why is it different?
      >>
      >Thanks,
      >>
      >Tom
      >>
      >>
      >>

      Comment

      • bruce barker

        #4
        Re: Event question

        it doesn't. MyEvent is just a collection of delegates. if exposed as as
        public property, intellisence knows how to add code to create the
        delegate. it names a method OnEventName and uses:

        EventName += new EventHandler(On EventName);


        this is all with subscribing to an event. this is just a naming
        convention that vs uses. i generally use a lambda function or create an
        anonymous delegate as I think this is often cleaner if the amount of
        code in the delegate is small (couple lines).

        at some point the code (often a control) must actually raise the event,
        so the delegate(s) will be called. this is when OnRaiseEvent is called.

        if you write asp.net controls, the control has interfaces you can
        implement, that will be called when it time to raise an event (like
        OnChange or OnClick).

        -- bruce (sqlwork.com)


        tshad wrote:
        "bruce barker" <brucebarker@di scussions.micro soft.comwrote in message
        news:CAB6BA8B-925F-45B5-9336-F6F556FBBEF0@mi crosoft.com...
        >events are pretty simple.
        >>
        >an event is just a collection of delegates (functions to call).
        >if you have an object that wants to raise events, that object defines a
        >public event (any name you want). when the object wants to raise events it
        >calls the delegates stored in its event object. the event will be null if
        >no
        >delegates have been added. by convention, the routine that raises the
        >event
        >is called OnEventName, and is code like:
        >>
        >public EventHandler MyEvent;
        >private void OnRaiseMyEvent( EventArgs e)
        >{
        > EventHandler handler = MyEvent;
        > if (handler != null)
        > handler(this, e);
        >}
        >>
        >
        But in your example you use something different. How does MyEvent know it
        is tied to OnRaiseMyEvent? The only place I can see that is in the
        OnRaiseMyEvent function:
        >
        EventHandler handler = MyEvent;
        >
        Whe is that executed to connect the two?
        >
        In my code:
        >
        public event EventHandler A1;
        public event EventHandler B1;
        public event EventHandler C1;
        >
        Even with out setting up the onA1, onB1 and onC1 - the system seemed to use
        those names as defaults since in my other page that used the control,
        intellisense was using them.
        >
        In you example, you use OnRaiseMyEvent and not OnMyEvent. Does it matter?
        You said it was a convention.
        >
        I am just trying to understand it here. I don't see anywhere where it says
        something like: MyEvent uses OnRaiseMyEvent.
        >
        >some point in your code when you want to fire the event, it would call
        >OnRaiseMyEvent .
        >
        In the case of a user control, I assume it would be on the User control on
        the aspx page (or another ascx page).
        >to subscribe to an event, your code addes a delegate (with the correct
        >signature) to the event, via the syntax of the language. in c# its one of
        >the
        >following:
        >>
        > myObject.MyEven t += new EventHandler(my Function);
        > myObject.MyEven t += delegate(object o, EventArgs e)
        >
        Do you use both of these? or just one?
        >
        Thanks,
        >
        Tom
        > {
        > // code goes here
        > }
        > myObject.MyEven t += (o,e) =>
        > {
        > // code goes here
        > }
        >>
        >>
        >-- bruce (sqlwork.com)
        >>
        >>
        >"tshad" wrote:
        >>
        >>I am just getting started with events and had a couple of questions on
        >>why
        >>they do what they do.
        >>>
        >>If you have a textbox and you want to handle an event you can just do:
        >>>
        >> this.TextBox2.T extChanged += new
        >>EventHandler( TextBox2_TextCh anged);
        >>and then have the function.
        >>>
        >> void TextBox2_TextCh anged(object sender, EventArgs e)
        >> {
        >> Response.Write( this.TextBox2.T ext);
        >> }
        >>>
        >>Here the event is created and tied to the control and function.
        >>>
        >>But if you do it in a user control, the function doesn't seem to be tied
        >>to
        >>anything.
        >>>
        >>For example, in my .ascx page I could have this:
        >> public event EventHandler BubbleTextChang ed;
        >> //protected void OnBubbleTextCha nged(EventArgs e)
        >> //{
        >> // //if (BubbleTextChan ged != null)
        >> // //BubbleTextChang ed(this, e);
        >> //}
        >>>
        >>Where I normally wouldn't comment out the function, so it isn't tied to
        >>anything (or doesn't seem to be). And the function seems to always need
        >>the
        >>word "on" in front of it.
        >>>
        >>If I had 3 events (and no functions) they would show up in intellisense
        >>with
        >>"on" in front of them:
        >>>
        >> public event EventHandler A1;
        >>>
        >> public event EventHandler B1;
        >>>
        >> public event EventHandler C1;
        >>>
        >> public event EventHandler D1;
        >>>
        >> public event EventHandler E1;
        >>>
        >>If I only had the above and nothing more, in my aspx page my intellisense
        >>would show:
        >>>
        >>onA1
        >>onB1
        >>onC1
        >>onD1
        >>onE1
        >>>
        >>Yet in my first example, the event was TextBox2.TextCh anged no
        >>TextBox2.onTe xtChanged or the event that was tied to that:
        >>TextBox2_Text Changed wasn't onTextBox2_Text Changed.
        >>>
        >>Why is it different?
        >>>
        >>Thanks,
        >>>
        >>Tom
        >>>
        >>>
        >>>
        >
        >

        Comment

        • tshad

          #5
          Re: Event question

          But that was my question. I am not trying to be difficult here, just trying
          to understand what you HAVE to do versus what CAN you do.

          Convention, means that you don't have to do something - just that everyone
          agrees that this is the way we are going to do it. Required means that you
          have to do it this way.

          If I have a control to put on my page like so:
          *************** *************** *************** *
          public partial class TextBox : System.Web.UI.U serControl
          {
          public string Text
          {
          get { return this.TextBox1.T ext; }
          set { this.TextBox1.T ext = value; }
          }
          public bool AutoPostBack
          {
          get { return TextBox1.AutoPo stBack; }
          set { TextBox1.AutoPo stBack = value; }
          }

          protected override void OnInit(EventArg s e)
          {
          this.TextBox1.T extChanged += new
          EventHandler(Te xtBox1_TextChan ged);
          base.OnInit(e);
          }
          void TextBox1_TextCh anged(object sender, EventArgs e)
          {
          OnBubbleTextCha nged(e);
          }
          public event EventHandler BubbleTextChang ed;
          protected void OnBubbleTextCha nged(EventArgs e)
          {
          if (BubbleTextChan ged != null)
          BubbleTextChang ed(this, e);
          }
          }
          *************** *************** *******

          And on my .aspx page I have the control as:

          <uc1:TextBox ID="TextBox1" runat="server"
          OnBubbleTextCha nged="TextChang ed" AutoPostBack="t rue" />

          And the .aspx.cs page is:
          *************** *************** ************
          namespace UserControlEven ts
          {
          public partial class _Default : System.Web.UI.P age
          {
          protected void TextChanged(obj ect sender, EventArgs e)
          {
          Response.Write( "In .aspx page: " + this.TextBox1.T ext);
          }
          }
          }
          *************** *************** ***************

          This works fine.

          But if I change the first set of code to:

          *************** *************** ***************
          void TextBox1_TextCh anged(object sender, EventArgs e)
          {
          OnRaiseBubbleTe xtChanged(e);
          }
          public event EventHandler BubbleTextChang ed;
          protected void OnRaiseBubbleTe xtChanged(Event Args e)
          {
          if (BubbleTextChan ged != null)
          BubbleTextChang ed(this, e);
          }
          *************** *************** *************** *

          Now there is NO OnBubbleChanged function. But the page STILL works as if it
          is. If you look at the text box on the .aspx page, intellisense still shows
          OnBubbleChanged and when I put a break on the first line of the
          OnRaiseBubbleTe xtChanged function it stops there ??????

          If I change to the TextBox to:

          <uc1:TextBox ID="TextBox1" runat="server"
          OnRaiseBubbleTe xtChanged="Text Changed" AutoPostBack="t rue" />

          It does a post back, but nothing happens.

          Why is that?

          It now goes to a non-existant method. It is forcing the "On" to precede the
          event name, which is fine. But I am trying to understand why and why it
          goes to the new function name.

          Thanks,

          Tom

          "bruce barker" <nospam@nospam. comwrote in message
          news:ebaOX1$LJH A.2348@TK2MSFTN GP05.phx.gbl...
          it doesn't. MyEvent is just a collection of delegates. if exposed as as
          public property, intellisence knows how to add code to create the
          delegate. it names a method OnEventName and uses:
          >
          EventName += new EventHandler(On EventName);
          >
          >
          this is all with subscribing to an event. this is just a naming convention
          that vs uses. i generally use a lambda function or create an anonymous
          delegate as I think this is often cleaner if the amount of code in the
          delegate is small (couple lines).
          >
          at some point the code (often a control) must actually raise the event, so
          the delegate(s) will be called. this is when OnRaiseEvent is called.
          >
          if you write asp.net controls, the control has interfaces you can
          implement, that will be called when it time to raise an event (like
          OnChange or OnClick).
          >
          -- bruce (sqlwork.com)
          >
          >
          tshad wrote:
          >"bruce barker" <brucebarker@di scussions.micro soft.comwrote in message
          >news:CAB6BA8 B-925F-45B5-9336-F6F556FBBEF0@mi crosoft.com...
          >>events are pretty simple.
          >>>
          >>an event is just a collection of delegates (functions to call).
          >>if you have an object that wants to raise events, that object defines a
          >>public event (any name you want). when the object wants to raise events
          >>it
          >>calls the delegates stored in its event object. the event will be null
          >>if no
          >>delegates have been added. by convention, the routine that raises the
          >>event
          >>is called OnEventName, and is code like:
          >>>
          >>public EventHandler MyEvent;
          >>private void OnRaiseMyEvent( EventArgs e)
          >>{
          >> EventHandler handler = MyEvent;
          >> if (handler != null)
          >> handler(this, e);
          >>}
          >>>
          >>
          >But in your example you use something different. How does MyEvent know
          >it is tied to OnRaiseMyEvent? The only place I can see that is in the
          >OnRaiseMyEve nt function:
          >>
          > EventHandler handler = MyEvent;
          >>
          >Whe is that executed to connect the two?
          >>
          >In my code:
          >>
          > public event EventHandler A1;
          > public event EventHandler B1;
          > public event EventHandler C1;
          >>
          >Even with out setting up the onA1, onB1 and onC1 - the system seemed to
          >use those names as defaults since in my other page that used the control,
          >intellisense was using them.
          >>
          >In you example, you use OnRaiseMyEvent and not OnMyEvent. Does it
          >matter? You said it was a convention.
          >>
          >I am just trying to understand it here. I don't see anywhere where it
          >says something like: MyEvent uses OnRaiseMyEvent.
          >>
          >>some point in your code when you want to fire the event, it would call
          >>OnRaiseMyEven t.
          >>
          >In the case of a user control, I assume it would be on the User control
          >on the aspx page (or another ascx page).
          >>to subscribe to an event, your code addes a delegate (with the correct
          >>signature) to the event, via the syntax of the language. in c# its one
          >>of the
          >>following:
          >>>
          >> myObject.MyEven t += new EventHandler(my Function);
          >> myObject.MyEven t += delegate(object o, EventArgs e)
          >>
          >Do you use both of these? or just one?
          >>
          >Thanks,
          >>
          >Tom
          >> {
          >> // code goes here
          >> }
          >> myObject.MyEven t += (o,e) =>
          >> {
          >> // code goes here
          >> }
          >>>
          >>>
          >>-- bruce (sqlwork.com)
          >>>
          >>>
          >>"tshad" wrote:
          >>>
          >>>I am just getting started with events and had a couple of questions on
          >>>why
          >>>they do what they do.
          >>>>
          >>>If you have a textbox and you want to handle an event you can just do:
          >>>>
          >>> this.TextBox2.T extChanged += new
          >>>EventHandler (TextBox2_TextC hanged);
          >>>and then have the function.
          >>>>
          >>> void TextBox2_TextCh anged(object sender, EventArgs e)
          >>> {
          >>> Response.Write( this.TextBox2.T ext);
          >>> }
          >>>>
          >>>Here the event is created and tied to the control and function.
          >>>>
          >>>But if you do it in a user control, the function doesn't seem to be
          >>>tied to
          >>>anything.
          >>>>
          >>>For example, in my .ascx page I could have this:
          >>> public event EventHandler BubbleTextChang ed;
          >>> //protected void OnBubbleTextCha nged(EventArgs e)
          >>> //{
          >>> // //if (BubbleTextChan ged != null)
          >>> // //BubbleTextChang ed(this, e);
          >>> //}
          >>>>
          >>>Where I normally wouldn't comment out the function, so it isn't tied to
          >>>anything (or doesn't seem to be). And the function seems to always
          >>>need the
          >>>word "on" in front of it.
          >>>>
          >>>If I had 3 events (and no functions) they would show up in intellisense
          >>>with
          >>>"on" in front of them:
          >>>>
          >>> public event EventHandler A1;
          >>>>
          >>> public event EventHandler B1;
          >>>>
          >>> public event EventHandler C1;
          >>>>
          >>> public event EventHandler D1;
          >>>>
          >>> public event EventHandler E1;
          >>>>
          >>>If I only had the above and nothing more, in my aspx page my
          >>>intellisen se
          >>>would show:
          >>>>
          >>>onA1
          >>>onB1
          >>>onC1
          >>>onD1
          >>>onE1
          >>>>
          >>>Yet in my first example, the event was TextBox2.TextCh anged no
          >>>TextBox2.onT extChanged or the event that was tied to that:
          >>>TextBox2_Tex tChanged wasn't onTextBox2_Text Changed.
          >>>>
          >>>Why is it different?
          >>>>
          >>>Thanks,
          >>>>
          >>>Tom
          >>>>
          >>>>
          >>>>
          >>

          Comment

          Working...