Components parameters

Hi everyone,
In here;
(object sender,System.EventArgs e)

I do not understand anything from the parameters of C#'s components.
What is the logic of using these paramater for all components in C#

Would you please explain them to me

Thanks,
Mert




Answer this question

Components parameters

  • Palkupz

    mert-1 wrote:
    Hmm,
    I think I began to keep the idea but I wonder that when do a progammer use these paramater

    When you fool around with your code quite often, it can be helpful to use the "sender" instead of the exact name of your object.
    E.G. (In this case I want to have the text of cboDatabase, for which I can as well use the sender object converted to - the original type - a combobox) :

    private void cboDatabase_SelectedIndexChanged(object sender, EventArgs e)
    {
    string s = ((ComboBox)sender).Text;
    }



  • yamadhrama


    Can it be said that all components(objects which make the events released) in C# has one unique method and when they relase their event, then the method which is unique for them begins its actions Is it true
    For instace, in here;
    (object sender,System.EventArgs e)
    This parameter is unique for a button and,
    When we click the button, this buttons reference variable go and sit on the first parameter location of the method that it belongs to, is not it


  • curiousbeginner

    I may be biased, but I think our documentation gives a great introduction and overview of events ;-)

    http://msdn2.microsoft.com/en-us/library/awbftdfh(VS.80).aspx

    Michael Blome - Visual C# Documentation team


  • Jeong-hun

    mert-1 wrote:

    I mean that suppose that you create two buttons on your Form and both have buttonClick method and have the same parameter list and type. And when we click one of this two buttons, how can the button know that which method to invoke (first click method or second click method )

    You have to attach the correct method to the buttonClick event of each button.
    Sometimes this will be the same method (code the same), but most of the times you will end up with 1 method for Button1 and another method for Button2.



  • elfelf


    I mean that suppose that you create two buttons on your Form and both have buttonClick method and have the same parameter list and type. And when we click one of this two buttons, how can the button know that which method to invoke (first click method or second click method )


  • Moje_pink_Squirrel

    mert-1 wrote:

    Can it be said that all components(objects which make the events released) in C# has one unique method and when they relase their event, then the method which is unique for them begins its actions Is it true
    For instace, in here;
    (object sender,System.EventArgs e)
    This parameter is unique for a button and,
    When we click the button, this buttons reference variable go and sit on the first parameter location of the method that it belongs to, is not it

    The first parameter (the object called "sender") is always the object on which the event is being raised, that's true.
    I don't really know what you mean by saying "all components have one unique method", but an object has / can have more than 1 event, I just gave 1 example for a button and 1 for a combobox.

    (object sender, EventArgs e) looks the same for almost every event on the button, combobox etc you trigger an event on, but the type of the object can be different (not comparing 2 buttons, but it will be when you compare a listbox to a textbox) and the properties of the objects can/will be different (you can access all properties of the 'component' through the object called 'sender').
    So when you click on the button, the "sender" is behaving like a reference variable (allthough it's not explicitally declared as a 'by reference' parameter).



  • Nick Macis

    Hmm,
    I think I began to keep the idea but I wonder that when do a progammer use these paramater


  • TimFroglich


    Thanks very much for your all clarifications.

    Best wishes,
    Mert(JavaBoy)


  • Mouli

    Thanks, but any more idea


  • Rolek

    The sender is the object which causes the event to be raised. E.g. the button you clicked on or the combobox of which you changed the selected index. You can use those when you have 2 similar controls with the same behaviour, you attach the same eventhandler to the event and by using the "sender" object you can still get different results.
    e.g. ((ComboBox)sender).Text
    will give you the text shown in the combobox you triggered an event on, regardless its name.

    e is an addition for the EventArgs supplied by the framework. Most of the time you'll hardly use them.



  • Student_Jon

  • Components parameters