Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF Events
I read the event part of the book WPF4 unleashed but I didn't find what I'm looking for.
 
For exemple I have two windows (both are open).
 
I have a button on the first one and a textbox in the second (with a number as a content) .
 
Everytime I click the button I want the textbox's number increased.
 
Looking forward to your help and suggestions!
Posted 14-Apr-13 15:53pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

"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) {
        // something's wrong, handle it or fix the bug
    }
    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
  Permalink  
Comments
devillspdr at 14-Apr-13 22:52pm
   
Thx for your help. It works !!
Sergey Alexandrovich Kryukov at 14-Apr-13 23:33pm
   
No doubt. It's a pleasure to help someone who can actually use the advice and make things working, especially that quickly. Congratulations!
 
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 520
1 Mathew Soji 335
2 BillWoodruff 260
3 OriginalGriff 215
4 Afzaal Ahmad Zeeshan 188
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 14 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100