{"record":{"id":"138a096d045c66fb","repo":"NickeManarin/ScreenToGif","slug":"it-was-not-possible-to-move-the-current-selected-r","errorCode":null,"errorMessage":"It was not possible to move the current selected region to the closest monitor.","messagePattern":"It was not possible to move the current selected region to the closest monitor\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"ScreenToGif/Windows/NewRecorder.xaml.cs","lineNumber":1668,"sourceCode":"\n    /// <summary>\n    /// Move the selection region to the closest screen when outside of any.\n    /// </summary>\n    private void MoveToClosestScreen()\n    {\n        //If the position was never set.\n        if (_viewModel.Region.IsEmpty)\n            return;\n\n        var top = _viewModel.Region.Top;\n        var left = _viewModel.Region.Left;\n\n        var screen = Screen.FromRectangle(new Rectangle((int)_viewModel.Region.Left, (int)_viewModel.Region.Top, (int)_viewModel.Region.Width, (int)_viewModel.Region.Height));\n        var closest = _viewModel.Monitors.FirstOrDefault(f => f.Name == screen.DeviceName) ?? _viewModel.Monitors.FirstOrDefault();\n        //var closest = monitors.FirstOrDefault(x => x.Bounds.Contains(new System.Windows.Point((int)left, (int)top))) ?? monitors.FirstOrDefault(x => x.IsPrimary) ?? monitors.FirstOrDefault();\n\n        if (closest == null)\n            throw new Exception(\"It was not possible to move the current selected region to the closest monitor.\");\n\n        //To much to the Left.\n        if (closest.Bounds.Left > _viewModel.Region.Left - 1)\n            left = closest.Bounds.Left;\n\n        //Too much to the top.\n        if (closest.Bounds.Top > _viewModel.Region.Top - 1)\n            top = closest.Bounds.Top;\n\n        //Too much to the right.\n        if (closest.Bounds.Right < _viewModel.Region.Left + _viewModel.Region.Width)\n            left = closest.Bounds.Right - _viewModel.Region.Width;\n\n        //Too much to the bottom.\n        if (closest.Bounds.Bottom < _viewModel.Region.Top + _viewModel.Region.Height)\n            top = closest.Bounds.Bottom - _viewModel.Region.Height;\n\n        UserSettings.All.SelectedRegionScale = closest.Scale;","sourceCodeStart":1650,"sourceCodeEnd":1686,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif/Windows/NewRecorder.xaml.cs#L1650-L1686","documentation":"MoveToClosestScreen finds the monitor whose Name matches Screen.FromRectangle(...).DeviceName, falling back to the first monitor. If _viewModel.Monitors is empty (or no entry matches and FirstOrDefault is null), it throws — there is no monitor to relocate the region onto.","triggerScenarios":"_viewModel.Region is non-empty AND (_viewModel.Monitors.FirstOrDefault(name == screen.DeviceName) ?? _viewModel.Monitors.FirstOrDefault()) returns null. The Monitors collection is empty at the moment of the call.","commonSituations":"A monitor was disconnected while the recorder was open and Monitors was cleared during the display-change refresh; multi-monitor reconfiguration event emptied the collection; rapid dock/undock of a laptop left the cache cold.","solutions":["Re-enumerate _viewModel.Monitors before throwing so a freshly-attached display is detected.","Reconnect at least one display before moving the region.","If no monitor is available, gracefully reset _viewModel.Region to empty instead of throwing.","Trigger MoveToClosestScreen only after a confirmed Monitors refresh."],"exampleFix":"// before\nvar closest = _viewModel.Monitors.FirstOrDefault(f => f.Name == screen.DeviceName) ?? _viewModel.Monitors.FirstOrDefault();\nif (closest == null)\n    throw new Exception(\"It was not possible to move the current selected region to the closest monitor.\");\n\n// after\nvar closest = _viewModel.Monitors.FirstOrDefault(f => f.Name == screen.DeviceName) ?? _viewModel.Monitors.FirstOrDefault();\nif (closest == null)\n    throw new Exception(\"It was not possible to move the current selected region to the closest monitor. No display detected — reconnect a monitor and retry.\");","handlingStrategy":"validation","validationCode":"if (_viewModel.Monitors == null || _viewModel.Monitors.Count == 0)\n{\n    await MonitorEnumerator.EnumerateAsync();\n    if (_viewModel.Monitors.Count == 0) return; // nothing to move to\n}","typeGuard":"bool HasAnyMonitor => _viewModel.Monitors?.Any() == true;","tryCatchPattern":"try { MoveToClosestScreen(); }\ncatch (Exception ex) when (ex.Message.Contains(\"closest monitor\"))\n{ /* reset _viewModel.Region to empty or warn user to reconnect a display */ }","preventionTips":["Re-enumerate _viewModel.Monitors on display-change events before acting.","Make MoveToClosestScreen a no-op when no monitor exists instead of throwing.","Defer region-move until the Monitors collection is confirmed non-empty."],"tags":["monitor","recorder","display-change","multi-monitor"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}