Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code, I get the following error in the main program when I call a sup form.

System.InvalidOperationException: 


this.Hide();
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new FrmFoodPantry());


What I have tried:

this.Hide();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmFoodPantry());
Posted
Updated 5-Jul-22 5:21am

I do not think you should be calling the Application methods inside your main form. You should use something like:
C#
FrmFoodPantry pantryForm = new FrmFoodPantry();
pantryForm.Show();
 
Share this answer
 
Comments
PaulaJoannAllen 5-Jul-22 12:06pm    
I entered the code

//this.Hide();
FrmNewMonth pantryForm = new FrmNewMonth();
pantryForm.Show();
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new frmFoodPantry());

And I get a build error.
CS0246 Type or Namespace name 'FrmNewmonth' could not be found.
I have added the form and its code to the project, which shows up under references.
[no name] 5-Jul-22 12:07pm    
FrmNewMonth is not FrmFoodPantry
PaulaJoannAllen 5-Jul-22 12:46pm    
That was a coding error, and it should be FrmNewMonth. FrmNewMonth is a .exe project that was added using the 'Project.Add reference'. I know that this changes the original question, should I let this one be closed and ask it again?
[no name] 5-Jul-22 14:37pm    
You could just update your question with the correct information, especially everything related to the problem.
To add to what Richard has said, you already have a running application, so trying to run a second is never going to work - at best you'd end up with two message pumps, at worst, your app would behave very oddly...

Richard is spot on that you need to create and Sow a new form, but you also need to handle when that form closes.
If you call Show, then the new form is created, and the method you are in continues from the next line immediately - so unless you handle the Form.Closed event for the new form your user will have no "normal" way of closing your app at all since the main form is hidden and cannot be accessed by the user.

Either handle the Closed event and close the main form in the handler, or use ShowDialog and execution of the method will not continue until the new form has been closed.
 
Share this answer
 
Comments
[no name] 5-Jul-22 11:28am    
Thanks for the extra info.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900