Author Topic: How to fire a command by clicking a button on a form?  (Read 2204 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
How to fire a command by clicking a button on a form?
« on: August 30, 2012, 10:10:49 AM »
Hi.
I add a form to my app to allow user to input some data, and then I want to clicking a button to run a command. This is want I do.
(1) add a from to my project,I call it "UserInput", and put some controls on it. A button is on the form to fire a command.
(2) double click the button, and add the following codes.
Code: [Select]
private void btnDoor_Click(object sender, EventArgs e)
        {
            Autodesk.AutoCAD.ApplicationServices.Document doc =
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            //Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            doc.SendStringToExecute("Door",true,true,true);
        }
(3) to show the form, define a command
Code: [Select]
[CommandMethod("UserInput")]
        public void ShowInput()
        {
            UserInput frmUser = new UserInput();  //UserInput is the form class I added.
            Application.ShowModalDialog(frmUser);
        }

Now, I run the "UserInput" command, the form showed successfully, but when I click the button, I get a "Error" showing "InValidInput". What's this. Where is the problem? Did I pass the wrong arguments to the SendStringToExecute()? How to fix it?
« Last Edit: August 30, 2012, 10:15:50 AM by 闻仲 »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to fire a command by clicking a button on a form?
« Reply #1 on: August 30, 2012, 10:16:43 AM »
Hi,

Try locking the document:
Code: [Select]
        private void btnDoor_Click(object sender, EventArgs e)
        {
            Autodesk.AutoCAD.ApplicationServices.Document doc =
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (doc.LockDocument())
            {
                doc.SendStringToExecute("Door",true,false,false);
            }
        }
Speaking English as a French Frog

waterharbin

  • Guest
Re: How to fire a command by clicking a button on a form?
« Reply #2 on: August 30, 2012, 10:52:08 AM »
Hi,gile.
I have change the command string.
Code: [Select]
private void btnDoor_Click(object sender, EventArgs e)
        {
            Autodesk.AutoCAD.ApplicationServices.Document doc =
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            //Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            doc.SendStringToExecute("_Door ",true,false,false);
            this.Close();
        }
And it's OK now. I don't understand the reason here.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: How to fire a command by clicking a button on a form?
« Reply #3 on: August 30, 2012, 11:26:13 AM »
I think the underscore indicates a language independent version of the command
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie