0

I am creating a Windows form using VB.net

The form is part of a VSTO Excel Add-in

I move the form to a secondary display using:

Dim b As Rectangle = activeScreen.Bounds

Dim location As Point = Me.Location

Me.StartPosition = FormStartPosition.Manual

location.X = location.X + b.X
location.Y = location.Y + b.Y

Me.Location = location

where activeScreen is of type System.windows.forms.screen.

Before resizing, my form is size {Width = 1139 Height = 308}

I resize my form using

Me.size = newsize

where new size has value {Width = 2278 Height = 308}

However, the value of Me.size somehow jumps to {Width = 5695 Height = 770}

And on the screen, this is in fact what is seen, the form is now 2.5 times bigger than what I expect.

If I change the height of the form in this way, all works fine. Similarly, the above code works fine when I keep my form on the primary display.

The value of Me.DeviceDpi is 96 in both scenarios.

My primary display is scale 250% (recommended) and Display resolution 3200x1800 in the settings, and screen bounds is [Bounds={X=0,Y=0,Width=3200,Height=1800}

The secondary display is scale 100% (recommended) and Display resolution 1360x768 in the settings, and screen bounds is {X=8000,Y=938,Width=3400,Height=1920}

  • The measures you're describing don't make much sense. See the notes here: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103), about the VirtualScreen coordinates and other information. Don't skip the DpiAwaress part, it may become useful. -- Scaling a Form to Font can have *fancy* effects. – Jimi Aug 31 '21 at 20:11
  • Thanks Jimi for your comment. Had already considered the effect of DPI and scaling. However, following your link, and considering that my code is part of a VSTO Excel Add-in, I ended up on this page here: https://learn.microsoft.com/en-us/office/client-developer/ddpi/handle-high-dpi-and-dpi-scaling-in-your-office-solution#vsto-add-ins. Followed all the instructions there very carefully, all however to no avail. See answer below for what really helped – Andrew Goldman Sep 03 '21 at 12:21

1 Answers1

0

The answer to this question seems to bear no resemblance to the problem.

I was resizing my form as part of a response to Form_PreviewKeyDownevent

Moving the code to Form_KeyUp event, suddenly everything behaved correctly as it had done when running on the primary monitor.

In the same vain, part of my form contains a Label that I change and move dynamically. This form was simply disappearing when I move the form to the secondary display. Debugging size, location, bounds etc showed numbers consistent with all the other controls on the form. Tried calling BringToFront() thinking maybe some subtle scaling had caused the label to be moved behind another control, but this did not help either.

Solution / workaround - every time I change or move the label, remove it from the controls and re-add a newly created label. Once again, with this work around, all once again works like it did on the primary monitor.