Hi, Can anyone tell me how to disable Close(x) button in an excel application which is opened inside a wpf window.
Thanks & Regards, Ramana
11 Answers
Know the answer? Post it. Somebody with the same question will find it here.
VulpesRank #55 · 29.2K reputationPosted May 2, 2011, 6:24 AM EDT
+1
Well, you can try this Win32 code which (in theory at least) should disable the Excel Close button and which I've placed in a console app for testing.
However, when I tried it on Excel 2007 (Windows 7) it disabled the Close button only when the Excel window didn't have the focus. As soon as it had the focus, the Close button re-enabled itself!
Whether it will behave the same when it's a child of a WPF window remains to be seen:
Mahesh ChandRank #1 · 393.3K reputationPosted May 2, 2011, 10:28 AM EDT
0
Are you opening xls document in MS Excel within your WPF application or you using some other control to display Excel?
0
Sam HobbsRank #107 · 20.5K reputationPosted May 2, 2011, 2:43 PM EDT
0
There is likely a more appropriate solution for whatever you need to do instead of disabling the close button. Why do you need to disable the close button?
Even if there is a good reason to disable the close button, an experienced Windows developer would attempt to disable the close message instead of the close button. The close message can be sent in a variety of ways.
Ooops, I am sorry, Vulpes; this is implying that you are not an experienced Windows programmer and I know you and Abhimanyu are. I am trying to get Ramana to consider alternatives but I don't mean to diminish the experience and knowledge of others. I might be the one that misunderstands.
Ramana19 reputationPosted May 3, 2011, 12:40 AM EDT
0
No, I've opened excel file using Microsoft.Office.Interop.Excel.Application() and setting WPF window as the Parent window Using user32.dll.
0
VulpesRank #55 · 29.2K reputationPosted May 3, 2011, 2:30 AM EDT
0
If you're starting Excel using interop, then you could (indirectly) use Sam's approach to prevent the user from closing the workbook by handling the Application's WorkbookBeforeClose event and setting the Cancel parameter to true. If 'xlApp' is your Excel Application object the code would be:
Ramana19 reputationPosted May 3, 2011, 2:39 AM EDT
0
Hi Vulpes , Thanks for ur reply. The code which u have posted will ensure that the WorkBook will not be closed , even I should not be able to close the Excel Application also.
Thanks & Regards, Ramana
0
VulpesRank #55 · 29.2K reputationPosted May 3, 2011, 3:05 AM EDT
0
To leave yourself the option of closing the workbook without killing the process, I'd just make the 'cancel' statement subject to some condition which is initially true but you can set to false:
VulpesRank #55 · 29.2K reputationPosted May 2, 2011, 6:24 AM EDT
Whether it will behave the same when it's a child of a WPF window remains to be seen:
To re-enable the Close button call:
EnableMenuItem(hMenu, SC_CLOSE, MF_ENABLED);
Abhimanyu K VatsaRank #14 · 51.7K reputationPosted May 2, 2011, 4:37 AM EDT
Ramana19 reputationPosted May 2, 2011, 4:43 AM EDT
Abhimanyu K VatsaRank #14 · 51.7K reputationPosted May 2, 2011, 5:24 AM EDT
http://stackoverflow.com/questions/3211518/remove-closex-button-from-wpf-window
http://stackoverflow.com/questions/743906/how-to-hide-close-button-in-wpf-window
Mahesh ChandRank #1 · 393.3K reputationPosted May 2, 2011, 10:28 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted May 2, 2011, 2:43 PM EDT
Even if there is a good reason to disable the close button, an experienced Windows developer would attempt to disable the close message instead of the close button. The close message can be sent in a variety of ways.
Ooops, I am sorry, Vulpes; this is implying that you are not an experienced Windows programmer and I know you and Abhimanyu are. I am trying to get Ramana to consider alternatives but I don't mean to diminish the experience and knowledge of others. I might be the one that misunderstands.
VulpesRank #55 · 29.2K reputationPosted May 2, 2011, 3:01 PM EDT
However, a problem with trapping WM_CLOSE is that the user would then have no visible indication that the window couldn't be closed.
Ramana19 reputationPosted May 3, 2011, 12:40 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 3, 2011, 2:30 AM EDT
xlApp.WorkbookBeforeClose += WorkbookBeforeCloseHandler;
private void WorkbookBeforeCloseHandler(Excel.Workbook Wb, ref bool Cancel)
{
Cancel = true;
}
Ramana19 reputationPosted May 3, 2011, 2:39 AM EDT
Thanks for ur reply. The code which u have posted will ensure that the WorkBook will not be closed , even I should not be able to close the Excel Application also.
Thanks & Regards,
Ramana
VulpesRank #55 · 29.2K reputationPosted May 3, 2011, 3:05 AM EDT
private void WorkbookBeforeCloseHandler(Excel.Workbook Wb, ref bool Cancel)
{
if (someCondition) Cancel = true;
}
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts