// Method with the same name as the class is a "constructor". // // This method makes a new PaintbrushControlModel. Every time you make a new PaintbrushControlModel, // you have to provide it with a Control (something on the screen that a user can click on) and // an IPaintbrushEventHandler (something that defines the methods in the IPaintbrushEventHandler interface). public PaintbrushControlModel(Control control, IPaintbrushEventHandler handler) { _control = control; _handler = handler; // MouseUp, MouseDown, and MouseMove are a special kind of variable called a "delegate". // A "delegate" is a place to which you can attach an event handler. // // The method "HandleMouseUp" is an event handler (it's a method that handles an event). // When I say "_control.MouseUp += HandleMouseUp", I'm saying, "Add my HandleMouseUp method // to the list of event handlers to which you send a MouseUp event." // // This is so that lots of different event handlers can listen to a single event, without // "getting in each other's way". I could add an event handler to the control's MouseUp // delegate, and someone else could add an event handler to the control's MouseUp delegate, // and both of those event handlers would get called when the mouse button was released. _control.MouseUp += HandleMouseUp; // HandleMouseUp is an event handler -- a method that knows how to handle MouseUp events. // // This is another way of making a "method that knows how to handle MouseUp events". It is // called an "anonymous function" or "a lambda" (for historical reasons). // It says, "Make an anonymous function that takes an object called 'sender' and some MouseEventArgs // called 'e', and show a message with MessageBox.Show(). // _control.MouseUp += (sender, e) => MessageBox.Show("second event handler" + e.Location); // This is another event handler (also a lambda). It's only different in that it calls // the second parameter "args" instead of "e", and says "third event handler" instead of "second event handler" // _control.MouseUp += (sender, args) => MessageBox.Show("third event handler" + args.Location); // "I want to listen to the MouseDown event of this control with my HandleMouseDown event handler method." _control.MouseDown += HandleMouseDown; // "I want to listen to the MouseMove event of this control with my HandleMouseMove event handler method." _control.MouseMove += HandleMouseMove; }
// Method with the same name as the class is a "constructor". // // This method makes a new PaintbrushControlModel. Every time you make a new PaintbrushControlModel, // you have to provide it with a Control (something on the screen that a user can click on) and // an IPaintbrushEventHandler (something that defines the methods in the IPaintbrushEventHandler interface). public PaintbrushControlModel(Control control, IPaintbrushEventHandler handler) { _control = control; _handler = handler; // MouseUp, MouseDown, and MouseMove are a special kind of variable called a "delegate". // A "delegate" is a place to which you can attach an event handler. // // The method "HandleMouseUp" is an event handler (it's a method that handles an event). // When I say "_control.MouseUp += HandleMouseUp", I'm saying, "Add my HandleMouseUp method // to the list of event handlers to which you send a MouseUp event." // // This is so that lots of different event handlers can listen to a single event, without // "getting in each other's way". I could add an event handler to the control's MouseUp // delegate, and someone else could add an event handler to the control's MouseUp delegate, // and both of those event handlers would get called when the mouse button was released. _control.MouseUp += HandleMouseUp; // HandleMouseUp is an event handler -- a method that knows how to handle MouseUp events. // // This is another way of making a "method that knows how to handle MouseUp events". It is // called an "anonymous function" or "a lambda" (for historical reasons). // It says, "Make an anonymous function that takes an object called 'sender' and some MouseEventArgs // called 'e', and show a message with MessageBox.Show(). // _control.MouseUp += (sender, e) => MessageBox.Show("second event handler" + e.Location); // This is another event handler (also a lambda). It's only different in that it calls // the second parameter "args" instead of "e", and says "third event handler" instead of "second event handler" // _control.MouseUp += (sender, args) => MessageBox.Show("third event handler" + args.Location); // "I want to listen to the MouseDown event of this control with my HandleMouseDown event handler method." _control.MouseDown += HandleMouseDown; // "I want to listen to the MouseMove event of this control with my HandleMouseMove event handler method." _control.MouseMove += HandleMouseMove; }