How to use the EventArgs of events

1

Maybe the question was very comprehensive, but I wanted to know how to use EventArgs of some components, such as EventArgs and Button_Click .

Explaining the question a little better, for example. I'm creating an application in which I use BackgroundWorker and Timer (Windows.Forms.Timer). And in the case of BackgroundWorker when I use RunWorkerAsync passing a parameter I can get the value passed in the parameter and use it in the function since the variable receives the cast appropriate. As in the code below:

public void execBackgroundWorker()
{
    BackgroundWorker bgwMain = new BackgroundWorker();
    bgwMain.DoWork += BgwMain_DoWork;

    int iValor = 1234;

    bgwMain.RunWorkerAsync(iValor);
}

private void BgwMain_DoWork(object sender, DoWorkEventArgs e)
{
    int valorParam = (int)e.Argument;
    Console.Write(valorParam);
}

But in the case of Button , its click does not have a overload in PerformClick that allows you to pass some parameter, but I still have EventArgs in the method call. I understand that DoWorkEventArgs inherits from CancelEventArgs and CancelEventArgs inherits from EventArgs and that the implementation ends up allowing DoWorkEventArgs to store variable values as in the above code. But what would be the functionality of EventArgs in other events such as Click Button , Tick Timer , among several other events that use the generic form of EventArgs ?

What does EventArgs store in Click for it to be used?

    
asked by anonymous 03.11.2016 / 15:04

1 answer

2

In click of a button ? Nothing , at least not naturally.

This parameter is used to pass arguments (0, 1 or more) to the event. Possibly the biggest goal of centering everything within EventArgs is to always keep the same default signature in methods ( public void evento(object sender, EventArgs e ), so you can pass multiple arguments within an argument only (within EventArgs ). p>

Review in class MouseEventArgs , used in events MouseUp , MouseDown and MouseMove , it has the properties Button , Clicks , Delta , Location , X and Y . That is, several values are passed in the same parameter.

EventArgs is the base class of several others, such as MouseEventArgs , quoted in the example, or BackgroundWorker as you've already noticed. So it works with all other components, this allows you to create a EventArgs and pass values you set when the event is triggered.

So the answer to your question:

  

" But what would be the functionality of EventArgs in other events such as Button Click, Timer Tick, and many other events that use the generic form of EventArgs?

is depends . You will need to read the class documentation to understand what the goal is or what you can do with it. With the MouseEventArgs it is possible to capture which button the mouse was clicked, which position X and Y of the click, how many times it was clicked , among others.

    
03.11.2016 / 15:56