Best practice to terminate WorkItem when user click control box 'X'

Topics: CAB & Smart Client Software Factory
Mar 14, 2007 at 5:27 PM
I am hosting views in a modal WindowWorkspace. When the user presses cancel I can catch the event and properly terminate the work item, but if they click the little X in the top right corner the view just closes. I know I can hide the X, but I'd like to keep it if possible. I've looked for any events on the UserControl that would be helpful, but not seeing anything.

Anyone found a way to catch this event and respond properly in the Presenter?

Mar 24, 2007 at 12:22 PM
there is a smart part closing event on the workspace that u can use.

Mar 25, 2007 at 6:52 PM
Thank you for the reply. I see the event, it's too bad that I can subscribe to it from the presenter. I will need to catch the event in my WorkitemController and check with the various views if they are ready to be closed.

Thanks again for the suggestion,
Apr 3, 2007 at 4:24 PM
I have the same problem.
How did you decide to solve this issue?
Apr 4, 2007 at 5:19 AM
Edited Apr 4, 2007 at 5:20 AM
It depends where your view is being hosted. Let assume the view is being hosted in a win32 form. We could then capture the form closing in our view as follows:

        protected override void OnLoad(EventArgs e)
            // Capture the Event of the form Closing
            this.ParentForm.FormClosing +=new FormClosingEventHandler(this.ParentForm_FormClosing);
Apr 4, 2007 at 3:41 PM


Here's what we did to solve this problem.

I assume you're using the WindowWorkspace in the Infrastructure.Library project. If not, consider using it because you can modify the source code easily (you can modify the CAB code too, but then you have to recompile CAB).

In your WindowWorkspace declare an event:

public event EventHandler OnClosed;

In the GetOrCreateForm method, wire up the form's Closed event:

form.Closed += new EventHandler(Form_Closed);

Then provide the event handler in the WindowWorkspace:

void Form_Closed(object sender, EventArgs e)
    if (OnClosed != null)
        OnClosed(sender, e);

Now, in the Presenter or WorkItem where you create the WindowWorkspace and show the view, wire-up the Closed event:

WindowWorkspace ws = new WindowWorkspace();
ws.OnClosed += new EventHandler(WindowWorkspace_OnClosed);

Now you can direct the event handler to whatever code (method or event) you desire for closing the the WorkItem.
Apr 5, 2007 at 2:42 AM
Well done, Chris... well done.

Thanks for sharing your solution. I will directly rip off your exact design! :)

Apr 5, 2007 at 6:39 AM
I forgot one small, but important, detail.

You'll also need to add a delegate to the WindowWorkspace, for the OnClosed event.

public delegate EventHandler Closed;

That should do it.
Apr 10, 2007 at 10:35 AM
I think it's a silly question, but how do you do when in the same scenario, you haver more than one workitem and you want to call Terminate() method for each one???

Apr 10, 2007 at 3:42 PM

I'm trying to imagine the scenario you describe. I think if I needed to close more than one specific workItem when a WindowWorkspace closed, I'd probably have the event that responds to the Window closing fire off a second EventBroker event that those WorkItems would be responsible for listening to, to tell them to terminate. Chaining events is a pretty normal thing to do in those situations (chain of responsibility).
Apr 11, 2007 at 7:10 AM
Thanks chris.

I've had done it by this way but i'm not sure if it was a good practise.