5

I'm making an Autocad Plugin which runs fine using Windows Forms And I've created a user control (winforms) to replicate in any form I like in the future.

The question is

From the control's code, how do I get the instance of the application running that control?

(Probably a pure winforms problem)

Coditions:

Within the plug-in I can get the Autocad Application instance with no problem.

This user control is meant to be in a separate assembly (dll) to be referenced in the plug-in application, so it has no direct access to the application instance.


A little explanation about the scenario:

There's a Main Assembly being run by Autocad as a plug-in. That assembly has the Autocad application instantiated.

Now I have some useful form controls to work with Autocad, and they are in a separate assembly. (That's because I want to use them in as many different plug-ins I like).

So, Autocad runs main assembly, and main assembly runs controls of the separate assembly.

In order to work properly, those controls need to have access to the Autocad application wich is running the main assembly.

Today I use the application as a property in the controls, wich I must set before working with them. (If I forget to set that, exceptions are raised). Since I cannot have a control with a creator taking parameters.

I want the controls to detect their running application so I avoid that workaround.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • I am not sure about why you would want the instance of Application. Perhaps, if you could explain your scenario in short, then that would help. The System.Windows.Forms.Application class exposes static methods that may be of help to you. In that case have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.application(v=vs.100).aspx – Jatin May 05 '13 at 07:35
  • There it goes, added scenario. – Daniel Möller May 06 '13 at 17:16
  • I don't think Autocad api gives you access to underlying windows Application object. I had faced similar issue while developing a wpf application. I ended up creating a Application as shown in this post - http://stackoverflow.com/questions/13658711/caliburn-micro-in-no-application-object-mode-like-in-autocad-dll-plugin. If your screens don't interact with AutoCAD screens, then you can try a similar approach. – Jatin May 07 '13 at 02:49
  • I can get Autocad application without any problem, and I don't need Autocad to have access to windows application. I need control to have access to Autocad. There's probably some way to find the application wich is running the control. – Daniel Möller May 07 '13 at 18:38
  • Are you looking for the Control.FindForm method? http://msdn.microsoft.com/library/system.windows.forms.control.findform.aspx – Simon Mourier May 09 '13 at 06:56
  • Good. It's half the way there. I wonder if I could find some `Form.FindApplication` as well. Or `Form.FindRunningAssembly.FindCallingAssembly`. – Daniel Möller May 09 '13 at 16:50

1 Answers1

2

Please see the following code

public class MyCommands {

    [CommandMethod("NS", "TEST", "TEST", CommandFlags.Modal)]
    public void TestCommand() // This method can have any name
    {
        Form fromAutoCADAPI = new TestForm();
        Form independent1 = new TestForm();
        Form independent2 = new TestForm();

        //Using AutoCAD application
        Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(fromAutoCADAPI);

        independent1.Show();
        independent2.Show();

        //Using Windows Forms Application
        var count = System.Windows.Forms.Application.OpenForms.Count; //should be 3

        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(count.ToString());

    }
}

If this is what you already know, then may be you should paste some sample code that will help understand where specifically you are stuck in your code. This is the way I have used AutoCAD application and Windows Forms application. In case you wan't to grab WPF application then you can use

var application = System.Windows.Application.Current;
Jatin
  • 4,023
  • 10
  • 60
  • 107
  • Let's say `TestForm` is declared in a external assembly (dll), wich is not this plugin's assembly. And TestForm needs to use the `Autodesk....Application` instance. I could, from this code you showed, make something like `independent1.AcadApp = AcadAppInstance`. Ok – Daniel Möller May 09 '13 at 16:44
  • But, what I want is TestForm `itself` to find out the application. I mean `from inside TestForm code`. So, it would be like a `independent1.GetAcadApplicationRunningMe()`. Of course it would be used internally, because externally I can easily take the application. – Daniel Möller May 09 '13 at 16:47
  • I cannot test now, but I'll look to it later. Can I use that `System.Windows.Forms.Application` from inside a `UserControl`? Notice that I don't have an explicit Winforms application. I have only two libraries. One is the plug-in, wich calls the other containing controls. – Daniel Möller May 09 '13 at 16:55
  • I think there should be no problem with using System.Windows.Forms.Application in any UserControl. The above code was used in AutoCAD plugin (dll, and not Windows Forms project), so it should work in your scenario too. You won't be able to store the Application instance in your UserControl, but actually you don't have to. System.Windows.Forms.Application can be used anywhere to perform Application level tasks. – Jatin May 10 '13 at 04:49
  • hmmmm....I wonder if I can use `Autodesk....Application` inside the control as well... – Daniel Möller May 10 '13 at 13:07