Click here to Skip to main content
15,847,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My main form is frmFoodPantry, and it calls a subform called frmOpentotals. The only purpose of frmOpenTotals is to hide all other sheets and display the Totals sheet. This all works fine, but when the user closes the sheet using the 'X' on the spreadsheet, then the frmFoodPantry form opens all greyed out. It runs InitializeComponent(); properly but won't display the form properly.

I can make it work by putting a close button on the frmOpenTotals form, but that is a really ugly bandaid.

I could put it all in one file, which makes it large and hard to follow.

private void frmFoodPantry_Load(object sender, EventArgs e)
     this.Enabled = true;

In frmOpenTotals
private void app_WorkbookDeactivate(Microsoft.Office.Interop.Excel.Workbook Wb)
      frmFoodPantry pantryForm = new frmFoodPantry();


What I have tried:

Updated 18-Sep-22 19:32pm

1 solution

Your code shows that the Load handler displays the form and enables it: which implies that somewhere the form is disabled - which will cause it to be greyed out and be unable to respond to user input.

And you are creating a new instance of your frmFoodPantry when you try to Deactivate the second form. That's not right because that doesn't bring back the existing instance with anything that has been updated since the app started.

I can't tell you exactly what to do - I don't have access to your code - but basically frmOpenTotals shouldn't know that frmFoodPantry even exists, so it certainly shouldn't be trying to create one.

Instead frmFoodPantry should create frmOpenTotals, add a handler to it's FormClosed event, and display it using Show, then call Hide on itself. In the FormClosed event hander you added frmFoodPantry should call Show on itself.
Remove all the code that sets Enabled, and remove all code which tries to access frmFoodPantry inside frmOpenTotals.

That way, the main form hides when the subform is displayed, and reappears when it goes away.
Share this answer
PaulaJoannAllen 19-Sep-22 6:49am    
The problem is not showing the main form that was hidden; I can do that with a close button. The problem is when they click on the 'X' of the excel sheet; I want to close the subform automatically and show the main form that was hidden. I agree that I was going about it the wrong way, but I don't know where to find documentation on this. Maybe I need to insert a macro in the excel sheet, being still a newbie I just don't know.
PaulaJoannAllen 19-Sep-22 11:20am    
I solved it by not hiding the main form as you suggested, not showing the subform, and on closing the workbook I run a routine called in the before app_WorkbookBeforeClose event. I think it is solved with your help.
OriginalGriff 19-Sep-22 11:26am    

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