By Adam Nagy
I use a modal dialog to enable the user to select a template for a new drawing and then I try to open it in the OnOkClicked message. It does not matter if I call Form.ShowDialog() or Application.ShowModalDialog() (ShowModalWindow() for WPF windows), DocumentManager.Add() does not succeed.
[CommandMethod("ShowMyDialog")]
public void ShowMyDialog()
{
MyForm mf = new MyForm();
Application.ShowModalDialog(mf);
}
Calling DocumentManager.Add() from here
public partial class MyForm : Form
{
// ...
string docTemplatePath;
private void OnOkClicked(object sender, EventArgs e)
{
Document doc =
Application.DocumentManager.Add(docTemplatePath);
this.Close();
}
}
Solution
You cannot manipulate (create/remove/switch) documents from a modal dialog. This is as designed.
You could easily modify your code so that you create the new drawing once the dialog is closed.
Also, though DocumentManager.Add() may succeed from a command running in document context, we'd suggest that you do it from session context.
public partial class MyForm : Form
{
// ...
public string docTemplatePath;
private void OnOkClicked(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
Showing the dialog and calling DocumentManager.Add() from here
[CommandMethod("ShowMyDialog", CommandFlags.Session)]
public void ShowMyDialog()
{
MyForm mf = new MyForm();
if (Application.ShowModalDialog(mf) ==
System.Windows.Forms.DialogResult.OK)
{
Document doc =
Application.DocumentManager.Add(mf.docTemplatePath);
}
}