Modal Forms

Topics: CAB & Smart Client Software Factory
Oct 27, 2006 at 9:48 PM
originally posted by: cpearson

Hi

I need to show a form 'modally'. The form is accessible from the Shell application via a menu command.

What is the best approach to enable this functionality?

Craig
Oct 28, 2006 at 12:37 AM
originally posted by: bil_simser

You would create your view as normal but instead of adding the view like any other, you create a WindowWorkspace programattically and add the view to it. Then use a SmartPartInfo and set the modal property to true. Check out the CAB SDK for info for an example (I'll also post some code later if you need it, I'm on the road right now).
Oct 28, 2006 at 1:22 AM
originally posted by: cpearson

Hi Bil

Thanks for the reply. Here's what I did:


private void OnClick(object sender, EventArgs e) {
WindowsWorkSpace ws = new WindowWorkspace();
WindowSmartPartInfo info = new WindowSmartPartInfo();
info.Modal = true;
ws.Show(myForm, info);
}

I end up with an error message about not being able to add a control to a top level control.

I have no need to use any of the injection or object builder features of CAB. If you have any, that would be great!

Craig
Oct 28, 2006 at 2:49 AM
originally posted by: bil_simser

Here's a command handler that we use for a modal dialog box (an about box with version info, etc. on the product). You could simply call this in an OnClick, you don't need the CommandHandler decoration:

CommandHandler(ModuleConstants.CommandNames.AboutBox)
public void ViewAboutBoxHandler(object sender, EventArgs e)
{
// Create a child work item so we're not messing with modules main work item
WorkItem child = WorkItem.WorkItems.AddNew<WorkItem>();

WindowWorkspace windowWorkspace = new WindowWorkspace();
WorkItem.Workspaces.Add(windowWorkspace, WorkspaceNames.ModalWindows);

AboutView view = child.Items.AddNew<AboutView>();
view.Dock = DockStyle.Fill;

WindowSmartPartInfo smartPartInfo = new WindowSmartPartInfo();
smartPartInfo.Modal = true;
smartPartInfo.MaximizeBox = false;
smartPartInfo.MinimizeBox = false;
smartPartInfo.ControlBox = false;
smartPartInfo.Title = "About...";

// TODO: create some helper methods in the utility class
// to determine the extra goo around a windows form and to
// give us the center of the screen based on the size of a window
smartPartInfo.Width = view.Width + 10;
smartPartInfo.Height = view.Height + 40;
smartPartInfo.Location = new Point(100, 100);

// Modal show of the window
windowWorkspace.Show(view, smartPartInfo);

WorkItem.Workspaces.Remove(windowWorkspace);
child.Terminate();
}
Nov 28, 2006 at 9:04 AM
originally posted by: JKraft4PIT

I had this problem also.

myForm hmmm.... is that a form?

You can't add a actual form. The window smart part is to make a USERCONTROL appear as a window. It does this by adding your user control as the base control.

//Code from SmartPartPalceHolder.cs
public object SmartPart
{
.....
this.Controls.Add(spcontrol); \\spControl is myForm, and you can't add a TopLevel control as a gen control which a form is
OnSmartPartShown(spcontrol);
}

So you don't add a form to it, you add a usercontrol (ie smartpart) to the window workspace.

So your code would look like this.

private void OnClick(object sender, EventArgs e) {
WindowsWorkSpace ws = new WindowWorkspace();
WindowSmartPartInfo info = new WindowSmartPartInfo();
info.Modal = true;
TheUserControlToShow myControl = new TheUserControlToShow;
ws.Show(myControl, info);
}

Look good?

Jordon