How to handle the closing of a form.

Topics: CAB & Smart Client Software Factory
Sep 24, 2007 at 7:27 PM

When i create a SCSF project, and add a view with a presenter, a public procedure named OnCloseView is created in the Presenter of my view.
This procedure make a call to mybase.CloseView that in turn make the workspace that contains my view to close my view.

My question is : who should call this procedure (OnCloseView) ?

if the view will make the call when it is closed (or want to be closed) what i should put in this procedure ? should i remove here my view from the smartparts collection of the work item containing my view ? and what if my view is simply a form that is closed by pressing the x, here i will overrides OnClosed procedure that make a call to my presenter's OnCloseView that in turn tell the workspace to close my view again !!! (i will stuck in a loop).
Sep 26, 2007 at 7:48 PM

The OnCloseView method is a method generated by the Add View recipe which you can invoke when you want to close a view. This method already performs the tasks you mentioned:
  1. It calls the CloseView method in the Presenter base class, which finds the workspace that holds the view (in the current WorkItem) and then calls the IWorkspace.Close method on the view.
  2. The IWorkspace.Close method then removes the View from the SmartParts collection.

Regarding when to call the OnCloseView method: I suggest you to have your view forward a user event (i.e.: a “Close” button click) to the presenter and let the presenter invoke the OnCloseView method. This way, you can add extra logic in the presenter before closing a view. For instance, you can save the information in the view before closing it.

// in the view
protected OnCloseButton_Click(…)
// in the presenter
public void OnCloseButtonClick()
                // save the user data, validate data, etc
                OnCloseView(); // close the view
Please let me know if this helps.

Ignacio Baumann Fonay
Sep 27, 2007 at 3:56 AM
thinks , but the problem is that my view is simply a form, and when i close it (by pressing the x button at top right of the window), i want to handle the OnClosing event, that in turn delegate the closing to the presenter as you say in your post. now the presenter will call CloseView in base class, that in turn will tell the workspace to close my form (again !), so i will be stuck in a loop !