I am implementing a VSTO Excel Add-In which displays some modal dialogs. These dialogs are not shown as own windows in the windows taskbar. But under certain circumstances these dialogs are not are vanishing from the top of Excel and cannot got back by using the task bar!
The whole story (reproducable with Windows XP - 7, Excel 2007 - 2010):
- I do open Excel and create two or more new workbooks
- I do show a modal dialog, let's say via "MessageBox.Show" I open "notepad" and maximize its window
- I try to reactivate one of the excel workbook windows via the windows taskbar
- I expect: Excel will come up with my modal MessageBox to appear on top
- The actual result is: Neither the MessageBox nor any of the Excel workbooks comes up, when you click a workbook item in the windows taskbar!
Why???
I can reactivate Excel via Ctrl + Tab. Then my modal dialog / MessageBox is correctly on the top. It's easy to reproduce if you have Visual Studio and Excel. Please help!
Greets, Jörg
Code-Sample:
- Just create an empty Visual C# / Office / 2010 / Office 2010 Add-In
- Replace the content of "ThisAddIn.cs" by the following code:
-
namespace ExcelAddIn6
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
NativeWindow excelWindowThatIsTheOwner = null;
try
{
//Get excel main window to set owner (however does not help anyway)
excelWindowThatIsTheOwner = new NativeWindow();
excelWindowThatIsTheOwner.AssignHandle(new IntPtr(Globals.ThisAddIn.Application.Hwnd));
//Create two more workbooks to have more than one...
Globals.ThisAddIn.Application.Workbooks.Add();
Globals.ThisAddIn.Application.Workbooks.Add();
Globals.ThisAddIn.Application.WindowState = Excel.XlWindowState.xlMaximized;
//Show modal dialog (here: a message box, but )
MessageBox.Show(owner: excelWindowThatIsTheOwner,
text: "I am a modal MessageBox.\nNow bring another application to the foreground and then try to bring excel back via the windows taskbar...");
//Problem stays the same for modal forms
var myForm = new Form();
myForm.ShowInTaskbar = false;
myForm.ShowDialog(excelWindowThatIsTheOwner);
}
finally
{
//Cleanup
if (excelWindowThatIsTheOwner != null) excelWindowThatIsTheOwner.ReleaseHandle();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}