0

When I click on a button in e.g. a WinForms application, what information is passed to the EventArgs e of the event method? I'm just wondering because I'm using the as keyword to "convert" e to a mouse event in order to get the coordinates of the point where the button was clicked.

EDIT: In the following example I can convert the variable e to an object of the type MouseEventArgs, and I want to know to what other types of event arguments e can be converted.

private void someEvent(object sender, EventArgs e)
{
  int xCoord = (e as MouseEventArgs).X;
}
  • 3
    Debug it, look into it. And read the [Documentation](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onclick(v=vs.110).aspx) – Mark Aug 18 '14 at 12:46
  • You should debug it to look into the variable and then see what it contains. And in the link you see the parameter there and when you navigate to it you see that it is a base class. If you can convert it, it is a derived class from EventArgs and then the output is depending on which Event your raising. In your case it is probable a MouseClick. That's why you can convert it into MouseEventArgs. – Mark Aug 18 '14 at 12:56
  • 1
    possible duplicate of [.NET Events - What are object sender & EventArgs e?](http://stackoverflow.com/questions/1303145/net-events-what-are-object-sender-eventargs-e) – drneel Aug 18 '14 at 12:57
  • @drneel I looked through the answers and they don't contain the information that answers to this question should contain. –  Aug 18 '14 at 13:07
  • ANYTHING that inherits from `EventArgs` can be passed as `e`. What is passed is entirely down to the control and any code that calls it. By convention, if they can pass specific objects, it will be typed as such. The best you can do is check if e is of the custom type you want before doing anything special with it. – Deanna Aug 18 '14 at 14:41

4 Answers4

1

This depends on the event, The underlying windows messages for most winforms events have no such concept, so where ever the EventArgs were created will determine the type and the information it contains. It could be something from the framework or you can just make up your own class derived from EventArgs.

After .Net4.5 it doesn't even have to derive from EventArgs

James
  • 9,774
  • 5
  • 34
  • 58
1

You should use System.Windows.Forms.Cursor.Position: "A Point that represents the cursor's position in screen coordinates."

1

There are 2 parameters: a sender, and an EventArgs. The sender is the object that initialized the event. The EventArgs contains additional information about the event.

From MSDN

// This example uses the Parent property and the Find method of Control to set 
// properties on the parent control of a Button and its Form. The example assumes 
// that a Button control named button1 is located within a GroupBox control. The  
// example also assumes that the Click event of the Button control is connected to 
// the event handler method defined in the example. 
private void button1_Click(object sender, System.EventArgs e)
{
   // Get the control the Button control is located in. In this case a GroupBox.
   Control control = button1.Parent;
   // Set the text and backcolor of the parent control.
   control.Text = "My Groupbox";
   control.BackColor = Color.Blue;
   // Get the form that the Button control is contained within.
   Form myForm = button1.FindForm();
   // Set the text and color of the form containing the Button.
   myForm.Text = "The Form of My Control";
   myForm.BackColor = Color.Red;
}
drneel
  • 2,887
  • 5
  • 30
  • 48
  • I already know that, I just wanted to know more about the variable **System.EventArgs e** as it's called in your answer. I wanted to know which information can be retrieved from it. Please look at the edit of my question. –  Aug 18 '14 at 12:53
0

I'm using the as keyword to "convert" e to a mouse event

Don't do it. Why?

Because if someone uses keyboard (TABs + Enter) or Button.PerformClick call to trigger your button, the conversion will fail and your:

MouseEventArgs mouseArgs = e as MouseEventArgs ;

if (mouseArgs.MouseButton == MouseButton.Left) ...

will lead to NullReferenceException.

You can't guarantee that EventArgs e in OnClick event will be MouseEventArgs. It is just the most common scenario, but not the only one possible.

P.S.: As it has been already pointed by @terribleProgrammer you could use Cursor.Position static property to get the current cursor position in more independent and robust way.

But if the cursor information makes sense only if the button has been triggered by mouse, then you will have just to be careful and handle possible conversion failure:

MouseEventArgs mouseArgs = e as MouseEventArgs ;

if (mouseArgs == null)
{
    Logger.Log("mouseArgs is null");
    return;
}    

if (mouseArgs.MouseButton == MouseButton.Left) ...

EDIT:

There are three main ways to raise the event and three corresponding eventArgs classes:

  1. Using mouse - MouseEventArgs
  2. Using keyboard - KeyEventArgs
  3. Using PerformClick method - I am not sure, but it will probably be just plain EventArgs(to verify just raise it in such a way and take a look at e.GetType()).
  4. Whether there are any other scenarios that will raise it, I don't know. MSDN is of no use for such requests.

P.S.1.: And I want to repeat again - do not assume that e parameter will be of some specific type. Windows Forms API even does not guarantee that e will contain some concrete useful EventArgs-derived class.

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • The button is just about opening a file dialog and setting the filter to either excel files or plain text files, depending on where the button was clicked (left: excel, right: txt), so it only makes sense to click the button with the mouse. –  Aug 18 '14 at 13:04
  • @marfuc Maybe it is easier to just use different handlers for each button? Or is the mouse position important for subsequent processing? – Eugene Podskal Aug 18 '14 at 13:08
  • It's just that particular button that has a function that exceeds just being clicked. Everything I already have in the application where the code that I've posted in the edit of my question resides is already working perfectly. –  Aug 18 '14 at 13:18
  • So, basically you ask what are the possible ways to trigger button.click event and what are the corresponding event args for each of such scenarios, or what? – Eugene Podskal Aug 18 '14 at 13:22
  • No, the button hasn't really anything to do with my question. It's just an example, because I use the code postes in my edit in my application to get the position of the point where the button was clicked (Which is irrelevant to the question!). BUT, I want to know what other types of _EventArgs_, like MouseEventArgs can be retrieved from the parameter **e** of the onClick function. –  Aug 18 '14 at 13:25
  • @marfuc I've modified my answer, but I am not sure that it really contains what you are looking for. – Eugene Podskal Aug 18 '14 at 13:41
  • Thanks, the _P.S.1_ contains the only information that's somehow useful for me. –  Aug 18 '14 at 13:44