This week I finished up a custom Dynamics GP 2013 eConnect integration that I developed as a Visual Studio Tools AddIn. The customer wanted to have a GP menu item that would open the integration window, so I used the Menus for Visual Studio Tools library to add a custom menu item.
The integration worked great and I packaged it up for deployment at the client.
Except as I was doing some final testing and double checking, I noticed a strange behavior with the window. Sometimes when I opened the custom VST window, it would open "CenterScreen", which is what I wanted. But other times, it would open in the upper left corner of my screen.
I tested it a few more times, and it seemed like the first time it opened after launching GP, it would open centered, but then subsequently it would open in the upper left corner of my screen. I tried various settings to try and position the window in the center of the screen, like manually setting the window Location property. But after several more tests, it would ALWAYS open in the upper left corner. It was ignoring my manual Location setting in the code.
It drives me nuts to have windows open in random positions, and I can't stand windows that open in the upper left corner for no reason.
Baffled, I created a brand new VS Tools project with a test window. When I opened that window from GP, it worked fine. It was always centered. What was going on??
I then compared every single window property in my integration project against the test VST project. All of the window properties were the same. Yet they behaved differently.
And then I finally noticed the difference. In my test project, where the window would dutifully open CenterScreen every time, I saw that the icon in the upper left corner of the window was different. It didn't have the GP window icon. This meant that it was a standard Windows Form and not a Dynamics GP VST Form. Hmmm.
I then added a second window to the test project--this time a Dynamics GP Form. I set all of the window properties exactly the same as the Windows Form. I then launched the windows. Sure enough, the Dynamics GP Form opened in the upper left corner of my screen, while the Windows Form opened Center Screen.
This screen shot shows how the standard Windows Form opens CenterScreen, while the Dynamics GP Form opens in the upper left corner of the screen.
And if that isn't the cutest photo ever featured in a Dynamics GP blog post, I don't know what is...
It seems that once you modify the window Startup Position or Location settings on the Dynamics GP Form, it triggers some bug that prevents the window from positioning properly, ignoring the window properties. I tried changing the settings to different values, but none would reliably reproduce the CenterScreen equivalent.
So after dozens of tests, here is what I have found.
If you have StartPosition = CenterScreen, and if the Location value is 0, 0, the VS Tools window will always open in the upper left corner. It may position properly the first few times, but if you change the StartPosition setting or Location setting, it seems to consistently revert to the upper left.
If you add code to set the form Location property during the OnLoad event, the window will open center screen the first time, but then subsequent opens will reposition it to the upper left.
But, if you then change the Location to some value other than zero, such as 100, 100, then setting the Location property will work every time.
However, there appears to be one additional catch. Based on dozens of more tests, setting the Location only works if it is the first line in your Form Load event. If you have any other code that refreshes the form, the Location setting will be ignored the second and subsequent time the window is opened.
So to summarize:
1. Set the form Location property to a value other than 0, 0, such as 100, 100
2. Add this as the very first line in your Form Load event:
this.Location = new Point((Screen.PrimaryScreen.Bounds.Size.Width / 2) - (this.Size.Width / 2), (Screen.PrimaryScreen.Bounds.Size.Height / 2) - (this.Size.Height / 2));
After hours of testing, I believe that these two techniques will allow you to work around the VS Tools window positioning bug. And after another test, it looks like changing the Location property may not be necessary if the Location setting is the first line in Form Load.
I don't recall ever having this issue before, so I'm guessing it was introduced in GP 2013 or GP 2013 R2. Given how subtle it is, I don't have high hopes that it will be fixed any time soon.
Steve Endow is a Microsoft MVP for Dynamics GP and a Dynamics
GP Certified IT Professional in Los Angeles. He is the owner of Precipio
Services, which provides Dynamics GP integrations, customizations, and
automation solutions.
1 comment:
I got this before and posted this few years back, still Microsoft didn't resolve this!
http://mohdaoud.blogspot.com/2009/01/starting-dynamics-gp-addin-form_5307.html
Post a Comment