Thread: confused with args for event handler~

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    confused with args for event handler~

    howdy~

    i'm go my first step on C# on a small asp.net app. i found a tuto very useful and most of its code are neat and nice, but i really get confused on args for even handler, the code of hanlder is somewhawt like this below:
    Code:
    void EnterBtn_Click(Object Src, EventArgs E) {
                Message.Text = "Hello " + Name.Text + ", welcome to world of ASP.NETŁĄ";
            }
    what about that object and eventargs there ???

    thanx~
    Never end on learning~

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Thats more of a definition than a real coding issue. A well-formed Event-Handler always has two parameters:

    object sender
    EventArgs e

    Depending on the event it is handling, the handler might receive classes derived from EventArgs like MouseEventArgs for Mouse events or any other class. The object always contains the instance of a class that caused the event. For example if you have a button clicked event, the first parameter given will always be the button in question. This way you can write a single eventhandler for many buttons and still know which one was pressed. The second parameter contains all additional info. For Mouse events, you can get the current mouse coordinates and button states from the second parameter.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx nvoigt~

    but i also failed when i try to reference Src like this:
    Code:
    void EnterBtn_Click(Object Src, EventArgs E) {
                Message.Text = "Hello " + Name.Text + ", welcome to world of ASP.NETŁĄ";
                Src.Text = "fsdfs";
    }
    what's wrong here ???
    Last edited by black; 03-03-2004 at 12:10 AM.
    Never end on learning~

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    While you know that the sender must be a button and therefore have a text, your compiler does not. For the compiler, it is an object and objects don't have a Text property. You have to cast it to a button:

    (Sender as Button).Text = ...
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  3. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  4. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM