Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Application that runs a Windows Form Control

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.

like image 701
Daniel Möller Avatar asked Nov 02 '22 22:11

Daniel Möller


1 Answers

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;
like image 69
Jatin Avatar answered Nov 08 '22 04:11

Jatin