Problem regarding Modeless form

Problem regarding Modeless form

Anonymous
Not applicable
2,833 Views
6 Replies
Message 1 of 7

Problem regarding Modeless form

Anonymous
Not applicable

Hi ,

 

I got one problem while using a modeless form. In a modeless form , I simply put a button and the button event is  like below code:

 

Private

Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

 

Dim acDoc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

acDoc.Editor.Regen()

EndSub

 

Then I got the output:

 

Capture12.JPG

 

output is okay. It will be 'Regenerating model'. But my question is why it is coming with 'command:'. While using the same code with modal then output only show 'Regenerating model' and 'command:' is not appear with that.

 

Capture12.JPG

 

It would be great if anybody explain me the problem and tell me how to get rid of it.

 

Thanks  and Regards

Zakir

0 Likes
2,834 Views
6 Replies
Replies (6)
Message 2 of 7

norman.yuan
Mentor
Mentor

That is how modal dialog box and modeless dialog box dirrerentiate to each other:

 

For modal dialog box, once it shows, it stops AutoCAD after the command "tst" (it brings up the modal dialog box, I assume),  AutoCAD cannot go back to command-accepting ready status until the modal dialog box dismissed.

 

For the modeless dialog box, after your command "tst1" brings up the modelesss dialog box, AutoCAD returns command-accepting ready status and user can enter command while the dialog box still shows, hence the "Command:" prompt.

 

You can call acDoc.Edior.WriteMessage(vbCrlf) right before you call Editor.Regen() to move the command text to next line to begin with, I think, so the message generated by Editor.Regen() would not look like being issued by user-entered command, or you amy want to make the message even explicitly from your code like:

 

acDoc.Editor.WriteMessage(vbcr & "My app is to regen the drawing..." & vbcr)

ac.Editor.Regen()

Autodesk.AutoCAD.Internal.Util.PostCommandPrompt()

 

Make sure you call PostCommandPrompt() if your code somehow write to the command line, because it is modeless dialog box, AutoCAD should be ready to accept command anytime. Or user may be confused to see a blank command line (without "Command:" prompt).

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 7

DiningPhilosopher
Collaborator
Collaborator
With a modeless dialog box, the user can access and use AutoCAD while the dialog box remains visible, so AutoCAD behaves as it normally does.

The "Command: " means that AutoCAD is waiting for the user to enter a command.

Before you try to use a modeless dialog, you should take the time to learn the differences between modal and modeless, and the increased complexity of using the latter.
Message 4 of 7

Hallex
Advisor
Advisor

I used solution from Norman from other thread,

search for StartUserInteraction

Sample code:

        // by norman.yuan
        private void btnPick_Click(object sender, EventArgs e)
        {
     
                Editor ed=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                 // for modeless dialog use this
                //using (ed.StartUserInteraction(this)) //Calling StartUserInteraction(form) will hide the form automatically
                //{
                    // for modal dialog use this
                    this.Hide();


                    PromptPointResult res = ed.GetPoint("\nPick a point:");

                    if (res.Status == PromptStatus.OK)
                    {

                        //Populate text boxes with picked Point3d value

                        txtX.Text = res.Value.X.ToString();

                        txtY.Text = res.Value.Y.ToString();

                        txtZ.Text = res.Value.Z.ToString();

                        // your rest code here  //

                    }
                    // for modal dialog use this
                    this.Show();

            //}// for modeless dialog use this ( EndUserInteraction )


        }

 

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 5 of 7

Anonymous
Not applicable
@ Norman: Thanks for you explanation and suggestion. I understand now this properly. I want to ask you one more question regarding Modal form. I have a modal form and a button. With that button click event, first I want to close the modal form and then I want to do something in the active document. Is it possible? or I want to do something interactively from Modal form. It would be great if you have any suggestion about that. Thanks once again.

@DiningPhilosopher: Thanks for you suggestion.

@Hallex: Thanks a lot your code :). you are very kind. Always give me some code to start .
0 Likes
Message 6 of 7

norman.yuan
Mentor
Mentor

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.

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 7

Anonymous
Not applicable
@ Norman: thanks a lot for your long reply. I got a feeling from Hallex code that interactive action can be done from modal form. But I need to understand properly how can do that. Your long explanation will help me to get rid of some confusion
0 Likes