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)
CodePudding user response:
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
CodePudding user response:
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.