How to use Event Eventargs

Asked

Viewed 902 times

2

Maybe the question was too comprehensive, but I wanted to know how to use the EventArgs of certain components, such as EventArgs in the Button_Click.

Explaining the doubt a little better, for example. I am creating an application in which I use BackgroundWorker and Timer (Windows.Forms.Timer). And in the case of BackgroundWorker when using the RunWorkerAsync passing a parameter I can pick up 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 a Button, his click does not have a overload in the PerformClick that allows you to pass some parameter, but I still have one EventArgs in the method call. I understand that the DoWorkEventArgs inherits from CancelEventArgs and CancelEventArgs inherits from EventArgs and that implementation ultimately allows the DoWorkEventArgs stores the values of variables as in the code above. But what would be the functionality of EventArgs at other events such as Click of Button, Tick of Timer, among several other events that use the generic form of EventArgs?

What the EventArgs stores in the Click for it to be used?

1 answer

3


What the EventArgs store in the click to be used?

At the click of a button? Nothingness, at least not naturally.

This parameter serves to pass arguments (0, 1 or more) to the event. Possibly the biggest goal of centering everything within the EventArgs is to always keep the same default signature in the methods (public void evento(object sender, EventArgs e), this way you can pass several arguments within a single argument (within the EventArgs).

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

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

So the answer to your question:

"But what would be the functionality of Eventargs in other events like Button Click, Timer Tick, among several other events that use the generic form of Eventargs?"

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.