|
-
Dec 16th, 2009, 03:50 AM
#1
Thread Starter
New Member
Calling Control_MouseDown
Hello,
How to call the code below:
Code:
private void Control_MouseDown(System.Object sender,
System.Windows.Forms.MouseEventArgs e)
{
Control control = (Control)sender;
if (control.Capture)
{
TextBox1.Text = control.Name + " has captured the mouse";
}
}
from a Button1 Click event:
Code:
private void Button1_Click(object sender, EventArgs e)
{
//this code doesn't work - returns invalid arguments
Control_MouseDown(sender, control)
}
-
Dec 16th, 2009, 02:03 PM
#2
Addicted Member
Re: Calling Control_MouseDown
You need to match the signature. The MouseDown event requires that 2 things are passed to it:
1) An object (This is generally the control that raised the event)
2) An object of System.Windows.Forms.MouseEventArgs
So you could do this...
Code:
Control_MouseDown(sender, new System.Windows.Forms.MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 0,1,1,0));
It is hard to figure out what to give you for an answer as it isn't easy to understand what you are attempting.
If someone has helped you, please make sure to rate them. 
-
Dec 17th, 2009, 04:17 AM
#3
Thread Starter
New Member
Re: Calling Control_MouseDown
Thanks I get the point and do what I need
-
Dec 17th, 2009, 05:06 AM
#4
Re: Calling Control_MouseDown
The simple fact is that you shouldn't be calling that event handler directly.
csharp Code:
private void Control_MouseDown(Object sender, MouseEventArgs e) { Control control = (Control) sender; this.IdentifyMouseCapture(control); } private void Button1_Click(object sender, EventArgs e) { Control control = (Control)sender; this.IdentifyMouseCapture(control); } private void IdentifyMouseCapture(Control ctl) { if (ctl.Capture) { textBox1.Text = ctl.Name + " has captured the mouse"; } }
-
Dec 18th, 2009, 04:47 AM
#5
Thread Starter
New Member
Re: Calling Control_MouseDown
Hi,
Can you give me more info why shouldn't be calling event handler directly? I believe you, but i just want to understand it.
For me at this stage seems more complicated to call one more procedure - IdentifyMouseCapture.
-
Dec 18th, 2009, 05:28 AM
#6
Re: Calling Control_MouseDown
An event handler is a method so you certainly can call it directly if you want to. The thing is, by creating "fake" arguments for an event handler you can get into the situation where you're not dealing with the data you think you are. This is one of those situations where if you just make a rule not to call event handlers directly then you can never get it wrong. It's very simple to structure your code not to have to call an event handler so you may as well do so.
-
Dec 18th, 2009, 09:05 AM
#7
Thread Starter
New Member
Re: Calling Control_MouseDown
1. When I call an event handler I thought I can call arguments, like any other procedure. I didn't know these arguments are a kind of "fake" arguments. I still didn't learn the conception around arguments in event handlers, that's why in my first post I asked such a beginner question.
2. Do you know some proper articles/sample code around that topic? Especially about calling directly and not calling directly pros+/cons-?
-
Dec 22nd, 2009, 01:58 PM
#8
Member
Re: Calling Control_MouseDown
The "fake" parameter you are passing is the MouseEventArgs object that you are creating.
You will run into this situation pretty frequently. The pattern that works in most cases is to factor out the common code into it's own function and then call the re-factored function as needed (just as jmcilhinney suggested) instead of the event handler.
-
Dec 22nd, 2009, 05:42 PM
#9
Re: Calling Control_MouseDown
The arguments received by an event handler are created by the object that raised the event. If there's no event and you created the arguments yourself then they are obviously fake, becaue they don't represent a real event.
You don't need a tutorial for this because it's pretty simple: event handlers are supposed to handle events. If there's no event there should be no event handler. You can call one directly because it's still a method. Doing so saves you having to write a separate method but, as one of our main aims when writing code is clarity, we shouldn't create a less clear, possibly problematic situation to save a few keystrokes.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|