Jakub Sluka
Jakub Sluka

Reputation: 135

C# changing EventArgs for method

I am developing a Windows Forms application, and I would like to pass to the MouseClick event on Picturebox a parameter of type MouseEventArgs instead of EventArgs, but the debugger yells an error for delegate when I do so.

void PictureBox1Click(object sender, MouseEventArgs e)
{
    // Some code
}

///////
// imageBox
// 
this.imageBox.Location = new System.Drawing.Point(12, 12);
this.imageBox.Name = "imageBox";
this.imageBox.Size = new System.Drawing.Size(1758, 829);
this.imageBox.TabIndex = 0;
this.imageBox.TabStop = false;
this.imageBox.Click += new System.EventHandler(this.PictureBox1Click);
// 

Thanks in advance, Jackob

Upvotes: 1

Views: 2373

Answers (3)

Reza Aghaei
Reza Aghaei

Reputation: 125312

When the click event has been raised by a mouse click, if you handle Click event, then the run-time type of e is MouseEventArgs as it's already mentioned by Jimi in the comments.

But in some controls, like Button, you may raise Click event without an actual mouse click, for example by calling button1.PerformClick() or when the button is the AcceptButton of the form and you press Enter, or if you press mnemonic key combination for the button or when the button has focus and you press Space. In such cases, the run-time type of e is EventArgs.

If you are interested to mouse click and you want to receive e as MouseEventArgs, then you should handle MouseClick event.

For example:

picttureBox1.MouseClick += PicttureBox1_MouseClick;

And then:

private void PicttureBox1_MouseClick(object sender, MouseEventArgs e)
{

}

Upvotes: 1

Christopher
Christopher

Reputation: 9824

Wich events exists, wich arguments they take, and indeed when they are called and wich values they are given is entirely up to the writer of that class. As a mere user of this code, you can not change it.

You can create a subclass of Picature Box where you can add your own event "ClickWithExtraArguments" or something like that. But raising it in place of the default one might not be easy. Sometimes the "RaiseEvent[X]" code is marked protected, so you can override it. RaiseMouseEvent is one of those, but this really goes deep into Event handling.

What inforamtion are you tryting to give via the Event Args anway? Can you not simply retreive the information in the existing Click Event handler?

Upvotes: 1

User81772
User81772

Reputation: 66

You could catch the mouse click event from the parent control, where the mouseclick event args are passed, check if the mouse position is inside the picture box and continue from there?!

Upvotes: 1

Related Questions