A couple modal forms questions

Topics: CAB & Smart Client Software Factory
Oct 12, 2007 at 11:47 AM

I have a couple of question regarding the use of modal forms in SCSF.

1. I'm trying to implement a toggle function for a smartpart so that it can be toggled from beeing hosted in one of my "primary forms" (MyEmbedded workspace), or hosted on it's own Form (Modalworkspace). I'm able to make my smartpart to toggle from MyEmbeddedworkspace to a modal by just calling the modalworkspaces show function. But how should I do in order to make it to go back to the emvbedded from the modal?

2. How do you prevent or handle a forms closing event that is triggered by pressing alt+F4 or the x-button?
Oct 18, 2007 at 8:15 PM

In order to toggle views between Modal and Deck (for example) Workspaces, you may want to implement a common event that switches the views. The event publication should send the view to toggle as a part of its EventArgs.

//Injecting the RootWorkItem
private WorkItem _rootWorkItem;
public WorkItem RootWorkItem
       get { return _rootWorkItem; }
       set { _rootWorkItem = value; }
[EventSubscription(EventTopicNames.ToggleViews, ThreadOption.UserInterface)]
public void OnToggleViews(object sender, WorkspaceEventArgs eventArgs)
            object view = eventArgs.SmartPart; //setting the view to toggle
            IWorkspaceLocatorService locator = _rootWorkItem.Services.Get<IWorkspaceLocatorService>();
            IWorkspace wks = locator.FindContainingWorkspace(_rootWorkItem, view); //locating the container workspace
            if (wks != null)
                wks.Close(view); //close the view from its container workspace
                if (wks is Microsoft.Practices.CompositeUI.WinForms.DeckWorkspace)
                    RootWorkItem.Workspaces[WorkspaceNames.ModalWindows].Show(view); //show the view in the modal workspace
                    RootWorkItem.Workspaces[WorkspaceNames.RightWorkspace].Show(view); //show the view in the right deck workspace

Regarding your second question, the WindowWorkspace class contains an event called SmartPartClosing() that is triggered whenever a SmartPart is closed. However, when closing a view within a WindowsWorkspace it stays in a state not suitable for being reused. What you could do is manage the view toggling in a custom WorkItem and/or saving and retrieving its State; or, in ultimately instance, make your own MyWindowWorkspace class which would inherit from WindowWorkspace.

There’s an interesting post about creating views using WorkItems that you may want to check out:

Please let me know if this helps.

Ignacio Baumann Fonay
Oct 19, 2007 at 5:42 PM
Thanks for your answer and the link you provided!