Devasena said:
Hi,
I would like to add a method to the Mouse_Move events of controls on a
form from other assembly. From one assembly if i click a button that should
inturn add a given method to the mouse_move events of other controls on
another form.
e-g:
Namespace1. Form1 has a Textbox Textbox1
Namespace2. Form2 has a button button1.
From Namespace2.Form2 when i click button1, i would like to add a
method to the mousemove event of Textbox1 of Namespace1.
I am aware that it can be done using reflection,but the posts i looked at
had lot more other stuffs and i am confused..
There are at least a few different ways to accomplish this. Keep in
mind that an event is just a special class member with an "add" and a
"remove" method, and that the argument is just a delegate instance
matching the event signature.
So, the most straightforward approach would be to simply provide methods
in Form1 to allow the subscription:
class Form1 : Form
{
private TextBox textBox1;
public void AddTextBox1MouseMoveHandler(MouseEventHandler handler)
{
textBox1.MouseMove += handler;
}
public void RemoveTextBox1MouseMoveHandler(MouseEventHandler handler)
{
textBox1.MouseMove -= handler;
}
}
class Form2 : Form
{
private Form1 form1;
private void button1_Click(object sender, EventArgs e)
{
form1.AddTextBox1MouseMoveHandler(form1TextBox1_MouseMove);
}
private void form1TextBox1_MouseMove(object sender, MouseEventArgs e)
{
// your handler code here
}
}
If you would like to be a little fancier, you can actually declare an
event in Form1 to do the same instead:
class Form1 : Form
{
private TextBox textBox1;
public event MouseEventHandler TextBox1MouseMove
{
add
{
textBox1.MouseMove += value;
}
remove
{
textBox1.MouseMove -= value;
}
}
}
class Form2 : Form
{
private Form1 form1;
private void button1_Click(object sender, EventArgs e)
{
form1.TextBox1MouseMove += form1TextBox1_MouseMove;
}
private void form1TextBox1_MouseMove(object sender, MouseEventArgs e)
{
// your handler code here
}
}
Note that both of the above approaches cause the event handler to be
subscribed directly to the textBox1 event. This is potentially more
efficient, but it has the problem that the textBox1 reference can now
"leak" out of the Form1 class (the sender is the textBox1 reference, not
the form1 reference), when it really ought to be hidden from the client
code.
IMHO, a better approach is to have Form1 publish an entirely new event,
and forward the event to subscribers:
class Form1 : Form
{
private TextBox textBox1;
private MouseEventHandler _textBox1MouseMove;
public event MouseEventHandler TextBox1MouseMove
{
add
{
if (_textBox1MouseMove == null)
{
textBox1.MouseMove += textbox1_MouseMoveProxy;
}
_textBox1MouseMove += value;
}
remove
{
_textBox1MouseMove -= value;
if (_textBox1MouseMove == null)
{
textBox1.MouseMove -= textbox1_MouseMoveProxy;
}
}
}
private void textBox1_MouseMoveProxy(object sender, MouseEventArgs e)
{
MouseEventHandler handler = _textBox1MouseMove;
if (handler != null)
{
// fix up MouseMoveArgs here if desired
handler(this, e);
}
}
}
class Form2 : Form
{
private Form1 form1;
private void button1_Click(object sender, EventArgs e)
{
form1.TextBox1MouseMove += form1TextBox1_MouseMove;
}
private void form1TextBox1_MouseMove(object sender, MouseEventArgs e)
{
// your handler code here
}
}
Note that in the above, the sender is your Form1 instance, not the
TextBox instance. This has the advantage of keeping the TextBox
instance private to Form1, as it ought to be. However, since
MouseEventArgs provide mouse coordinates relative to the sending
control, depending on how you're actually using the event you _might_
want to create a new MouseEventArgs instance that converts the
coordinates so that they are relative to the Form1 instance, to preserve
the usual semantics of the event.
I would think any one of the above solutions could work well for your
purposes, depending on exactly what you're trying to do.
Pete