In the case of AutoCAD, we may use modal form in 2 ways:
1. Open the modal form, and then do something to the active document with user action (clickingbutton, for example) while the modal form remains showing. For example, clicking one button to change all texts' heigh, or clicking another button to find all blocks with particular name...Depending on what is to do, sometimes modeless form may be used.
2. Open a modal form to collect user input and close it to do something based on user input. This would be most common use for modal form. In this case, I usually use this type of cod estructure:
[Command("DoSomething")]
public static void DoSomething()
{
//Get user input, assume I have UserData class to hold input information
UserData iputs=GetUserInput()
if (UserData==null) return;
//Do something according to user input (UserData)
....
}
private static UserData GetUserInput()
{
UserData data=null;
using (myForm flg=new MyForm())
{
DialogResult res=Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm);
if (res==DialogResult.OK)
{
data=dlg.UserData //The form expose user input collected as a read-only property
}
}
return data;
}
I assume that you know that when showing as modal form, Form.Close() (or click "x", or Form.Dialogresult==DialogResult.OK/Cancel does not actually close the form, instead, it hides the form. That is why one need to use the modal fomr with using{} block, or call Form.Dispose() to actually close the form.
So, to your question, yes, you can close modal dialog box (usually set the close button's DialogResult property to OK/Cancel, or in the click event, set the Form.DialogResult to OK/Cancel) and keep the form alive in the memory, so that you can retrieve the user input from the form (in the case of OK, of course). The form is disposed only after you retrieved the user input.
During the form showing, you may want to let user interact with AutoCAD,such as picking an point in AutoCAD (instead of letting user to enter point's coordinate number by number.
You can use Editor.StartUserInteraction():
private void MyButton_Click(...)
{
Editor ed=Autodesk.....MdiDocument.Editor;
bool picked=true;
using (ed.StartUserInteraction(this)) //This will automatically hide the form
{
Point3d pt
picked = UserSelectPoint(out pt)
if (picked)
{
txtPiontX.Text=pt.X.ToString();
txtPiontY.Text=pt.Y.ToString();
}
} //Form will be back here
}
That is, Editor.StartUserInteraction() automatically hide the modal form and show it back when the EditorUserInteraction is disposed.
However, using this you need to set reference to WPF stull (presentionframework/presentationcode, system.xmal, windowsbase...) even you only use Windows.Forms.
Also, you may have to mix AutoCAD namespace in the form's code, unless you write your code very carefully. If you do not have a modal form on top of another modal form, simply call form.Visible=false/true would be OK to allow user to interact with AutoCAD during modal form showing stage.