I created a new Windows Form in my DLL Extension and try to make it open on a command:
public class DialogWindows
{
[CommandMethod("Form Dialog", CommandFlags.Modal)]
public void TestBrowserCommand()
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form1);
}
}
But the IDE show me an error (The name 'form1' does not exist in the current context.)
What is wrong?
You have to use a Instance of your Dialog... not the class...
I miss a line like
form1 dlg = new form1;
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(dlg);
But if form1 is the Instance and not the className, than you are probably missing a using directive. Hard to say, without seeing the whole thing.
I just add to my project a new Windows Form, fill it with buttons, testboxes and so on.
Than I want this form to show in AutoCAD, when I use the command. There is nothing more to show(
The form1 you are using in thiis line:
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form1);
has to be declared somewhere.
Without showing your code, I have to guess, what it is and where it comes from...
The possibilities which I can think off are:
form1 is the Classname.
Than you have to use (as mentioned above)
form1 dlg = new form1();
and show the dlg.
Or:
form1 is the instance, than is the Question, where it is declared...
Possibility one would be static in the class (which would then normally be called Form1).
Then you have to use here Form1.form1;
Possibility two would be not static in the class Form1.
Then you need an Instanse of the class Form1, and use this
Also
Form1 f1 = new Form1()
And use then f1.Form1;
Or form1 is declared somewhere in your Command class... then the IDE should be happy....
You see, there is a lot someone might to see to help you. (The next question would be, if your command class and your Form are in the same dll and the same namespace....)
You have to use an instance... not a classname, as said before.
So you have to change:
[CommandMethod("Form Dialog")]
public void TestBrowserCommand()
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(MainApiDll_CSharp.Form1);
}
to
[CommandMethod("Form Dialog")]
public void TestBrowserCommand()
{
MainApiDll_CSharp.Form1 dlg = new MainApiDll_CSharp.Form1();
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(dlg);
}
Also you should consider to put your class DialogWindows in the same namespace then the rest of your project... this would than look so:
namespace MainApiDll_CSharp { public class DialogWindows { [CommandMethod("Form Dialog")] public void TestBrowserCommand() { Form1 dlg = new Form1(); Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(dlg); } } }
Also a good argument for descriptive and verbose naming (InterfaceTestForm vs. Form1). Helps to keep things straight when working on larger projects, and when migrating code you haven't looked at for a couple of years.
Best way to show a form into AutoCAD using .NET API is like below:
Application.MainWindow -- will make you form realize that it' parent is AutoCAD main editor
false -- will make sure that the form will not remember previous settings. As an example if you close your form in minimize mode or hide mode and when you call it again it will be minimized or hide and you will be in trouble somehow in that case..