WinForms focus issue in .NET 9
We have an issue when we create a WinForms form from the ribbon.
It works fine in .NET 4.8, 6.0 and 8.0:
but does not in 9.0, it types in the Excel spreadsheet below the form:
We have no idea how to debug this. Do you think it's an issue with .NET?
See this code to be able to reproduce: https://github.com/jewicht/ExcelDNA-Samples/tree/add-form-to-ribbon-example (Ribbon.sln is modified to show a form with a textbox.)
This sounds like a problem that has been discussed from time to time. See the threads here: https://groups.google.com/g/exceldna/c/WV5P4QVzo8M/m/QioOVV8ZAwAJ https://groups.google.com/g/exceldna/c/5GWoKylgZbA/m/RhBkvqTAAAAJ
This is perhaps the most relevant suggestion https://stackoverflow.com/questions/19458721/cant-type-on-a-wpf-window-in-a-vsto-addin
The following property must be changed before showing the popup window.
Globals.ThisAddIn.Application.Interactive = false;And then reset to true on closeGlobals.ThisAddIn.Application.Interactive = true;
I know this more as an issue with WPF, so I'm surprised that you are reporting this with WinForms. I also know this problem from previous .NET versions, maybe there has been an update to Windows Forms in .NET 9 which affects this.
Thanks for the hints. To try toggling Interactive, I had to find a way to block the execution on the form creation and display, and ShowDialog() did the trick by itself.
So in the end, I had to modify from
public void OnButtonPressed(IRibbonControl control)
{
new Form1 { Visible = true, TopMost = true };
}
to
public void OnButtonPressed(IRibbonControl control)
{
var form = new Form1 { Visible = false, TopMost = true };
form.ShowDialog();
}