简体   繁体   中英

Form.Show() doesn't work for 1 specific computer

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:

表格问题

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM