Handling window coordinates for 2 displays

My app saves and loads the windows position. If you had 2 displays and one is now off but not removed macOS and Xojo still think that there are 2 displays. The

[code]dim theRect as Realbasic.Rect
theRect = new REALbasic.Rect(PrefValueLeft, PrefValueTop, PrefValueWidth, PrefValueHeight)

Dim screenID as integer
For l as integer = 0 to screenCount - 1
if theRect.top >= screen(l).top and theRect.center.x > screen(l).left and theRect.center.x < (screen(l).left + screen(l).width) then screenID = l

The screenID is 1 and not 0 even if there is no second display. As result my main window isn’t showing up anymore. Does anyone have an idea how to handle this problem?

If the display is off but connected there is now way for the system to know(I think).
What I did for an app was to create a menu “Collect all windows” or similar that just move all windows to the main screen(0).

@Albin Kiland: very good idea. I’ll do that.