The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 5 of 5

Thread: Events c#

  1. #1

    Events c#

    Hi,

    I have the following code:

    void buttonClick(object sender, EventArgs e)

    {

    Button bn = sender as Button;

    if (bn.Name == "button1")

    {

    MessageBox.Show("Hey its button one");

    }

    }

    I am interested in the object sender and eventargs e

    From what I understand the object is the object that is raising the event, which is a button in this case. I can use the event as an entry point for several objects (again all buttons) and then I can test to see which button was clicked. I have done this in this code.

    A couple of questions:

    1. Is there anything else I would commonly use the object for?

    2. I am unclear on what if anything I can do with the eventargs or 'e'

    Thank you for any help or pointers with this.

  2. #2
    e is an object of type EventArgs. EventArgs is basically a collection of arguments that may apply to the type of event you are handling. The "generic" EventArgs doesn't do a whole lot, but some of the more specific types of events have different uses...

    For instance MouseEventArgs on a mouse move event can help you track the mouse's position on your form. To see what you can do with that object, try setting an event handler up in VS and type "e." inside your method to see what kinds of instance properties and methods you have access to via intellesense.

  3. #3
    thanks. I read up some more on this and saw that EventArgs has 4 main properties: Source, OriginalSource, Handled and RoutedEvent.

    I need to work some more on this especially with Handled as I can't see why at the moment its always seems to be set to false.

    thanks for the reply

  4. #4
    Well, I'd assume handled would be set to false by default. C# won't automatically set it to true for you. You'd have to do that programmatically by saying e.Handled = true; somewhere inside your method. You don't necessarily have use EventArgs within an event handler.

    Basically, they are set up the way they are with that specific method signature (void return type and arguments passed in) because they are set up via delegates. So when you set up the event handler, often by double clicking a control, or by using the events panel in VS, the IDE writes a little bit of code in the background for you. When you set something like this up by hand, and want to use an event handler, you may not always want to make full use of EventArgs... if you don't, you can just do the following:

    myEventHandler_Click(0, EventArgs.Empty);

    The 0 can be typed to any numeric type really, but whether you pass in a class, an intrinsic type (string, int, double, etc), they all derive from object. EventArgs is basically an enumeration. You can pass in an EventArgs object that is meaningful, but if you don't need it, but still want to make use of a specific method, then you can pass in dummy arguments that never get used.

    I guess what I'm saying is, if you need it, then use it. Otherwise, don't worry so much about it.

  5. #5
    Thanks again. I had assumed that handled would be set to true by the RoutedEvent when it had finished. I set a break point in the code and kept an eye on handled as I expected to see it change to 'true' once the event was complete but it remains at false. As you say its no biggie.

    I have an AS background and am just moving into c# so I am interested in more detail than I probably should be

    thanks again

    <Edit> actually just used the e.Handled to set it to true for a keyboard input that was not numeric. So I can see how to use it now.
    Last edited by skjc; March 2nd, 2008 at 07:32 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012