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.
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;
Related
I created a class library (I checked to make sure it was set to that in the Application tab of the properties window.) However i am now getting a
Program does not contain a static ‘Main’ method suitable for an entry
point
I added a program.cs and added an empty Main method, to no avail. There are about 10 .cs files that comprise this lib. Is that the issue?
The meaning of a class library is to provide classes that can be shared to other projects. It is not intended to be run as a standalone program. If you want to be able to run an application, I would advise you to create a Console Application, or if you want it to have a UI, to create a WPF or Windows Forms application.
I have a parent form that starts another application. When the application starts it should set the application as an mdichild of parent.
I have managed to get this to work with notepad using the code below.
Process proc = new Process();
proc.StartInfo.FileName = "notepad.exe";
proc.Start();
proc.WaitForInputIdle();
SetParent(proc.MainWindowHandle, this.panel.Handle);
The problem is that it doesn't work with all applications. Specifically it doesn't work with other win forms applications developed in c#, which is what I need it for.
Any help is appreciated, Thanks.
Edit: I have access to the code for the c# app i want to open as a mdi child.
OP: I have access to the code for the c# app i want to open as a mdi
child.
To open a form of other .Net windows application, you don't need to use SetParent and you can simply add a reference of that .Net Application (dll, exe or project) to your current project and then create an instance of the main form of that application and show it like other forms of your application.
If you have access to project of that application, you can add it to your current solution and use add a reference to your current project.
If you don't have access to project of the project of that application, you can add a reference to its exe/dll to your current project.
To learn more about add reference, read How to: Add or Remove References By Using the Add Reference Dialog Box
add .exe(for example another.exe) file as reference
add another.exe references // may be differ from your mdi form
add below code
another.Form1 fr = new another.Form1(); //another.exe has Form1 if not select form.
fr.MdiParent = this;
fr.Show(); // Display the new form.
I got following setup:
WPF_Application.exe
and
a DLL that contains a WinForms Window and an WPF Window.
The 'WPF_Application.exe' calls the WinForms window from the DLL and the WinForms Window creates an instance of the WPF window in the DLL.
That creates following exception:
Cannot create more than one System.Windows.Application instance in the same AppDomain.
Tried different things, but couldn't figure out how to fix it.
I fixed it by asigning the Application of the wpf window that is called to the Application.Current Window:
if (Application.Current == null)
{
MyApplication = new Application
{
ShutdownMode = ShutdownMode.OnExplicitShutdown
};
}
else
MyApplication = Application.Current;
System.Windows.Application is a singleton: its constructor must only be invoked once (including App.xaml de-xamlization), or exception is thrown.
I'm affraid there is not much you can do about it, except maybe check if Application.Current is set before starting your second application, and somewhat use that instance to load it.
Or you can create another AppDomain, and use it to launch the second application.
Make sure you only have one Application defined in XAML.
Visual studio creates an App.xaml when making a new WPF solution, if you have created a new .xaml file with the <Application></Application> tag, this will cause this error and the solution will build but fail at runtime.
Delete the duplicate application and use Window or UserControl to have multiple windows.
I have made a winform application for use within the company I work for( web site was not an option)
The user base of this application has increased and of course it has become a pain to upgrade everyone...even more now since users are now disallowed form installing their own applications.
I came up with the idea of keeping the application stored on a database to be loaded upon execution.
It all works fine, but it seems that the graphics have a pre-windows7 (on win7 machines) look to them. Why would that be happening?
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(assemblyPath);
Type type = assembly.GetType(typeName);
Form form = (Form)Activator.CreateInstance(type);
Application.Run(form);
Call Application.EnableVisualStyles before any controls are created, prior to loading the assembly.
in order to use the FolderBrowserDialog control in asp.net, i had to add a reference to System.Windows.Forms in my project.
i wrote this code:
FolderBrowserDialog f = new FolderBrowserDialog();
f.ShowDialog();
but this error occured:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
any help?
If you're trying to use this control in an ASP.NET application, you're going to be sadly disappointed.
You can't use WinForms controls on ASP.NET Pages. You should check out the FileUpload control instead (it will allow your users to pick a file and upload it to the site).
If you're actually building a WinForms application (not ASP.NET), then the fix is quite easy (and you should fix your question and tagging):
public static void Main()
Becomes:
[STAThread]
public static void Main()
Keep in mind though, Visual Studio usually adds this to your generated code when you create a WinForms project.
You cannot use Windows Forms controls in ASP.NET. And in fact using the FolderBrowserDialog from ASP.NET doesn't make a lot of sense, since it is a webpage. Web applications can't get direct access to a user's filesystem. If you want to get a file from the user you should use a FileUpload control.