"Suggestions"… it really sounds funny, in case of such a "problem".
First of all, the expression "assign an event" demonstrates the lack of understanding of the nature of things. The events are never "assigned". The are declared in some class, and you never can assign anything to it explicitly, moreover, you can never initialize it. The event instance is actually created each time you add a handler to its invocation list. When some code already added one event handler, it may seem that when you add another handler to the same instance of the event you modify it, but, surprisingly, this is not the case.
Event instanced are immutable. Each time you add a handler (with += operator) or removed it (-+ operator), you actually create a brand new event instance with modified invocation list. One of the reasons for such behavior, as in the case of other immutable objects (string is a well-known example) is multithreading optimization.
Finally, you can
invoke an event instance, but only in the declaring class. This one of the important limitation making event instanced different from "regular" delegate instances, an
important fool-proof feature. As you are talking about the event
System.Windows.Controls.Primitives.ButtonBase.Click
,
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click%28v=vs.95%29.aspx[
^], you cannot invoke this event directly (however, indirectly, you can write a class derived from
Button
to call the protected method
System.Windows.Controls.Primitives.Button.OnClick
,
http://msdn.microsoft.com/en-us/library/system.windows.controls.button.onclick%28v=vs.95%29.aspx[
^]).
I think, for now it's all you need to know on event use. Importantly, no "assignments".
All you need to do is "+=", to be used to add an event handler. The event handler will be a delegate instance invoked during the invocation of the event instance. As delegate instanced carry reference to the object on which the handling method is called, the handler method can be a method of any other object, not related to the one owning the event instance. If this is a window, it can be the same window or any other one.
The only "problem" will be passing the references needed for handling between forms. In your case, the most convenient way would be passing the reference to the event instance, as it is usually already declared as
public
or
internal
. You need to add the handler in the content where both the reference to the event instance and the window owning the
TextBox
are known. Something like:
MyWindowmWithButton buttonWindow =
MyWindowWithTextBox numberWindow =
buttonWindow.myButton.Click += () => {
int current;
if (!int.TryParse(numberWindow.MyTextBox.Text, out current) {
}
numberWindow.MyTextBox.Text = (++current).ToString();
};
This will work. At the same time, it's not good to create such context where the internal detail of both windows are visible, it would violate perfect encapsulation of windows. The finer solution would be this: you create some interface with a method returning the referenced to event instance and implemented it in the windows owning that event. Pass just the interface instance to other form and add the handler in its context.
—SA