2

I am developping an Excel addin in C#. This addin contains panels with some buttons to open hovering Forms.

I have been notified that this feature doesn't work anymore with one computer in my company. It uses Excel Office 2016 to the latest version.
Here is what happens:

Form issue

The grey box is what should be a Windows Form, but for some reasons it doesn't load up. The user clicks a button to open it, and it just stays grey with no controls, no events, no response of any kind.

Now, here is the code that should open the Form :

private void _Form_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{   
    // ActiveForm() returns basically a UserForm with some methods to fit my specific needs
    ActiveForm().ShowMyForm(_columnButton); // --> ClickEvent
}

public void ShowMyForm(object parent = null)
{
    ActiveForm().StartPosition = FormStartPosition.CenterScreen;

    if (parent != null)
        ComputeLocation(parent);

    IntPtr handle = new IntPtr(Excel.Hwnd);
    ActiveForm().Show(new WindowWrapper(handle)); // --> Shows a grey box
}

public class WindowWrapper : IWin32Window
{
    private IntPtr _hwnd;
    public WindowWrapper(IntPtr handle)
    {
        _hwnd = handle;
    }

    public IntPtr Handle
    {
        get { return _hwnd; }
    }
}

I'm giving this code just in case, but I'm not really sure it is faulty.

The exact same code works perfectly fine on any other machine, and the problem occurs with any version of my addin.
I've checked it with several (stable) versions, and even with a beta version, the issue still occurs.

However, it appeared after the addin was reinstalled to a previous version on the computer. It isn't supposed to mess with anything external, but I believe it is important to point it out.
I'm using a msi installer generated with Wix 3.11 to install.

I have not modified the code, but here is what I have tried. I hope this isn't ridiculous, I'm kind of unexperienced with this:
- Uninstalling and reinstalling the addin,
- Repairing Office 2016 and then Uninstall/Reinstall when it wouldn't change anything,
- Updated the .NET version from 4.6 to 4.7.2 (the addin requires 4.6)
- Updated the graphic card drivers on the computer

In addition, I've tried to check logs to see if any kind of error was generated:
- My addin shows no error in the logs
- I've set up the Visual Studio project and started the addin on debug mode, with breakpoints and all --> no error of any kind
- I've checked the Windows Logs Events and found nothing related. I might have missed something since I'm not experienced to read them.

I have finally found a link that "did something" :
How to revert to an earlier version of office 2016

I reverted the Office version to 16.0.9330.2118, and the Forms started working as expected for around 30 minutes. Then I was notified that the issue had come back, even though nothing had been updated on the computer. It just "went wrong again". I updated Office to the latest version and it started working again until next morning.

As you can see, no error is generated, but swapping the Office version to a different one "does something". It looks like some kind of conflict with some Office libraries, but I have no idea what to do or how to deal with that.
Even though I've noticed a few topics that looked like my issue, they were all related to code and Forms management. I really believe this has nothing to do with code, even though I can't prove it for certain.

Can anybody help me with this? Even some thoughts on how to deal with this issue would be appreciated, since I'm really not comfortable with it.

EDIT:

So I kind of figured out what happened, but not entirely.
I did what @HansPassant told me and ran the VS debugger on my colleague's computer, step by step, and got it running again and again. It appeared what was causing the issue was the ComputeLocation method (see the original content).
Basically ComputeLocation worked like this (I simplified it a bit):

private void ComputeLocation(object parent)
{
    Control parentControl = (Control) parent;
    Point location = parentControl .PointToScreen(new Point(0,0));

    //Process location's coordinates here

    location = parentControl.PointToClient(location);

    ActiveForm().StartPosition = FormStartPosition.Manual;
    ActiveForm().Location = location;
}

I finally noticed that the issue would happen when using PointToClient. The Form would change its location but its content wouldn't.
I got the expected behavior upon removing the call to PointToClient.

I'd like to point out that this code is actually faulty (as opposed to what I was saying in the original part), because ActiveForm() is a topMost Form and has no parent. Using PointToClient in this context is a mistake because its location is always expressed in screen coordinates, although this is not what caused my issue here : in this context, PointToClient always returns a location that is almost the same as the screens coordinates.

I ended up removing permanently the call to PointToClient, clearing the issue. The content of the form followed its container again.

I still have no clue why PointToClient causes this behavior.
A few days later, another colleague got the exact same issue whereas nothing had been updated on his computer. The addin went from running normally on an excel workbook to displaying grey boxes on the next one.

I don't know if I should mark this question as solved since I was able to remove the issue, but do not fully understand what causes it.

Cedt
  • 181
  • 11
  • 2
    You'll have to drag a debugger to that machine to make any headway on this problem. Watch out for the UI thread burning (nearly) 100% core, controls only paint themselves when the UI thread is idle. – Hans Passant Jul 12 '18 at 13:47
  • @HansPassant Thanks for answering me ! Does starting the addin with Visual Studio in debug mode on the faulty computer is enough? Also, I can't breakpoint inside Show because it is a MSDN method, however I have noticed that when Show returns on a "normal" computer, there are holes for the controls to fill in the "grey box", and even some parts with colors, which I do not have on the faulty computer (as you can see on the screenshot), so should I really look into what comes after? It looks like the issue comes before that but I will look into it if you really think I should. – Cedt Jul 12 '18 at 14:06
  • 2
    The holes are normal when there is a painting problem. Not seeing them is not that encouraging a hint that this is actually a painting problem. But realistically, holes are pretty bad and does point at a nasty runaway UI thread problem. Too much guessing btw, that can easily send you into a rabbit hole that is hard to get out of. – Hans Passant Jul 12 '18 at 14:14
  • I understand, sorry about the guessing. Also, thanks for the heads up on the holes ! However, I believe I was wrong about the painting. I checked: actually what really happens is that the code gets out of Show (at this point you can see the form with holes), closes a progress bar, which is another thread, and then the forms displays correctly for a non-faulty computer. There are no Paint method in my ActiveForm or any non-MSDN related classes. On the faulty computer, it just stays grey all the way after Show. There are no custom controls to paint. Does it answers your first comment? – Cedt Jul 12 '18 at 14:34
  • @Cedt you said that this issue came back after a while. I wonder if that machine has a high density display (4K?) and the user had changed scaling for that display. Office add-ins run in System Aware DPI awareness context, so controls might be painted in the wrong locations. – Tanya Solyanik Jul 17 '18 at 19:14
  • @TanyaSolyanik thanks for your answer. The display isn't 4K and the user had not changed the scaling. I did however made progress on this issue. See my EDIT on the original post if you're interested about this. – Cedt Jul 19 '18 at 12:29
  • "one computer in my company" Throw that computer away. – Davesoft Jul 19 '18 at 13:47

1 Answers1

0

I believe I have found what caused the issue here. The problem "spread" to 2 more computers in my company and I was able to fix them with the correction I made, even though I didn't know the real cause.
One more issue appeared over time: ToolStrips controls weren't displayed anymore in the add-in forms.

Ultimately, one of my coworkers had to update his Windows version and it totally wiped out all of the issues.
I checked, all 3 computers were running on Windows 10 Version 1709.
I found a link about this version issues, but I'm not entirely sure this is exactly what caused the faulty behaviors:
https://support.microsoft.com/en-us/help/4340917/windows-10-update-kb4340917

The other machines were quickly updated to the Windows 10 April 2018 upgrade, and everything is running fine again on every versions of the add-in.

Cedt
  • 181
  • 11