.NET

Reply
Contributor
NikWach
Posts: 17
Registered: ‎09-19-2013
Message 1 of 8 (373 Views)

Using Application.ShowModalDialog

373 Views, 7 Replies
10-02-2013 05:22 AM

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?

Contributor
Matti72
Posts: 25
Registered: ‎09-28-2004
Message 2 of 8 (370 Views)

Re: Using Application.ShowModalDialog

10-02-2013 05:31 AM in reply to: NikWach

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.

Contributor
NikWach
Posts: 17
Registered: ‎09-19-2013
Message 3 of 8 (368 Views)

Re: Using Application.ShowModalDialog

10-02-2013 05:35 AM in reply to: Matti72

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(

Contributor
Matti72
Posts: 25
Registered: ‎09-28-2004
Message 4 of 8 (361 Views)

Re: Using Application.ShowModalDialog

10-02-2013 05:46 AM in reply to: NikWach

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....)

 

 

Contributor
NikWach
Posts: 17
Registered: ‎09-19-2013
Message 5 of 8 (358 Views)

Re: Using Application.ShowModalDialog

10-02-2013 05:52 AM in reply to: NikWach

Here is the project

Contributor
Matti72
Posts: 25
Registered: ‎09-28-2004
Message 6 of 8 (351 Views)

Re: Using Application.ShowModalDialog

10-02-2013 06:10 AM in reply to: NikWach

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);
		}
	}
}

 

*Expert Elite*
dgorsman
Posts: 5,035
Registered: ‎10-12-2006
Message 7 of 8 (334 Views)

Re: Using Application.ShowModalDialog

10-02-2013 07:41 AM in reply to: Matti72

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.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


Mentor
mzakiralam
Posts: 225
Registered: ‎11-09-2012
Message 8 of 8 (288 Views)

Re: Using Application.ShowModalDialog

10-03-2013 12:28 AM in reply to: NikWach

Best way to show a form into AutoCAD using .NET API is like below:

 

Capture1.JPG

 

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..

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community