Pls Help : Adding a method to the Mouse Move events of a form from

D

Devasena

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..

Please help me ..

Regards

Devasena
 
P

Peter Duniho

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
 
D

Devasena

Duniho , Thank you very much for the solution. I tried this and working well
in a general case.

Am sorry, i should ve explained little more about the scnerio

This is with respect to user control & design time support. The solution
you have given is perfect, but pls tell me how can we write the same with
out having much code in assembly1.

It would be much appreciated if we have the event related code in assembly2.
so that my end developer will not worry about the event declaration &
generation in assembly1.

Please help me.

Thanks
Devasena
 
P

Peter Duniho

Devasena said:
Duniho , Thank you very much for the solution. I tried this and working well
in a general case.

Am sorry, i should ve explained little more about the scnerio

This is with respect to user control & design time support. The solution
you have given is perfect, but pls tell me how can we write the same with
out having much code in assembly1.

It would be much appreciated if we have the event related code in assembly2.
so that my end developer will not worry about the event declaration &
generation in assembly1.

I'm afraid I don't understand the question. What is "assembly1" and
"assembly2"? Do you mean "assembly1" is where "Namespace1.Form1" is
found and "assembly2" is where "Namespace2.Form2" is found?

I cannot recommend any solution involving less code that what I've
already suggested. Without reflection, there is no way for Form2 to
attach itself in any way to the private textBox1 in Form1 other than to
modify Form1 in _some_ way.

And if you are going to modify Form1, you might as well do so in a good,
maintainable way by providing new class members that expose the event
and only the event. And there's not really any more concise way to do
that other than what I've already suggested.

Pete
 
D

Devasena

Hi Peter,

I had acheived this via CodeDom. using codedom i could able to generate
code and using delegates it worked.

Thaks for your support.

Regards
Devasena
 
D

Devasena

I solved this issue using CodeDom . I was able to create automatic codes and
with delegates, this issue is solved.

Thanks for your support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top