Is it possible to open winform form from dependency? - c#

I have a visual studio windows forms application that has multiple projects, each having multiple forms. The project that loads on startup calls another project's form and closes its own. Referring to the startup program as "setup" and the other as "main". I have a scenario where I want to skip setup and be able to return to it later. Since setup is a dependency of main, and not the other way around, I cannot seem to create an instance of the setup form. Is this the case, or am I doing something wrong?
The call from setup to open main is as so
this.Hide();
frmDemo demo = new frmDemo();
demo.ShowDialog();
this.Close();
I want to do the same thing from main to setup form, but I am having trouble.
Essentially it should just be
frmSetup setup = new frmSetup();
setup.show();
But this isn't working because it cannot find the form.

Well if you want to reference Setup from Main then you need to add a reference in Main.
The problem then is that you will create a circular dependency, which is bad design (and I think you will also get compiler errors).
There are some ways to get around this, but the best approach it to make your Main program the first one that starts-up (not setup). Then during startup it launches the set-up form and waits until it is finished and then continues. And then when it needs to launch setup again, then it is no problem.
So you are effectively reversing the dependency.

Related

How do I run other windows forms while enabling a reference? Once I connect my reference, the application gets stuck on only one form

visual studio problem
I am trying to run different windows forms but once I enable my reference to TrackerLibrary, my Application.Run gets stuck on only being able to run the last form it was on before enabling my reference. Is there some way to fix this? (new to coding so I don't know if I'm missing anything)
The Application.Run method is what starts your application and the program will not go past that method call unless the user exits the application.
Using Application.Run(Form mainForm) will only allow you to start your application with 1 open form and the application will exit when that form is closed.
If you'd like to open more than 1 Form on start, consider using Application.Run(ApplicationContext context).
Application.Run Method documentation
Your application doesn't get "stuck on one form". You can open as many other forms as you want. What you can't do is close that first form without the application exiting. When you call Application.Run and pass a form, that method will return if and only if that form closes, at which point your Main method will also complete, if you have no more code after the Application.Run call.
If you want to be able to close a form without the application exiting then you cannot pass that form to Application.Run. What you can do instead is derive your own class from ApplicationContext and then create an instance of that class and pass it to Application.Run instead. You can then put whatever logic you want in that class to open and close as many forms as you like.
I'm not going to provide any code here because I don't know specifically what it is you want to do. Better that you follow the link above and work through the example it provides in order to understand the concept, then implement it as you need. You might also like to check out my own example of using a custom ApplicationContext here.

Running Projects Programmatically

I was wondering if it is possible to open a second project (in the same solution as the first one) by code in the first project.
For example i have one form application project and another console application project.
The form application starts and when the user clicks a button i want the console application to run and the form application to stop.
Or could someone tell me how to delete my application .exe file?
The projects don't need to be in the same solution to do that. Just use Process.Start to start the executable for another application, and then close the main form to end the current application.
If you don't want to run the code as an entirely different process then it may also make sense to have a 3rd project that is a "class library" that the other two projects could add a reference to. This would allow you to define common code used in either application, using classes that are generalized to be helpful in either project.
I'm not sure what you're trying to do; but Process class has Start and Kill methods that will let you launch / exit processes.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

How to run code right when a C# application starts?

I have put some code inside of the public MainWindow() {} but I kept getting some obscure XAML parsing errors as soon as I did that (not on my computer but on 3 others I've tried it on - yep!)
Is there the preferred way to run code AS SOON as t he application starts?
The theory is I want it to call home and ask it it's ok to start. If it's not, I want the app to close out. Call it a makeshift copy-protection :)
Under normal circumstances, WPF creates the Main method (the entrypoint of the application) for you. Your options
Create a handler for the Application.Startup event and put your code there. Alternatively, you can override the OnStartup() method.
If that's too late for you, put your code in the App's parameterless constructor (it probably doesn't exist, but you can create it).
If even that's too late, you can create your own Main() method. There are several ways how to do that. Probably the easiest is to put it in another class and tell Visual Studio you want to use this method in the project's properties.
On the other hand, you said you're getting some obscure XAML parsing errors. Maybe you should figure out what exactly do they mean?
You have Window.Loaded event in WPF.
But if if you want to check for run permission before application loads ( due some resource consuption or some business strategy) use a bootstrapper a separate small executable that first launched by mainexe and after if everything ok a bootstrapper runs main exe

Form not listed in Windows Service Project Start up objects in VS.net 2008

I have a windows service in VS.net 2008. Just to test the application before I install the service, I have a form (Form1.cs).
Now when I want to make this project as startup project and form1 as startup object,
I don't see the form one in the start up object list.
I just see the namespace of the Program.cs and not set
Please help
Presumably you don't have a
static void Main()
method in Form1.cs. (The method could return int and/or take a string[] parameter, and it doesn't have to be private.) That's what makes VS consider a class to be a potential entry point.
To be honest, it's be pretty odd IMO to have a form in the same project as a service. Why not have a separate project for it which would work in the normal way? Having multiple entry points is fine, but if one of them starts a service and the other opens a form instead, that's a little strange.

Spawning form in new process

I had originally created a windows form to be a dialog of my projects main form. Now the dialog is getting complex enough that it needs to be started in its own process. Is there a way to do this in code or do i need to create a new project and link my files to it?
I question the premise here - there is no reason to necessarily start a new "form" in a separate process. If the form is getting that complex, however, I would recommend simplifying it, if for no reason other than usability.
That being said, you can always launch a new process via Process.Start in code. If you want it to be in the same project, but start a separate process, you could launch the executing exe with a command line argument that allows you to switch which "form" is loaded at startup.
You will need to set up a communication layer (WCF using named pipes would probably be the best way to go).
But I would seriously question why you need a new process for your form? Make sure that whatever work you do on your form is done on a separate thread. That way you can have dozens of forms open but your app will remain very responsive.
It's not the best solution, but if you are trying to avoid a rewrite, a call to Application.DoEvents will pump the message queue and get you some responsiveness back if you have a whole lot of updates happening on your UI. Band aid solution to your problem though.

Categories

Resources