{"id":20,"date":"2009-07-09T20:41:46","date_gmt":"2009-07-09T20:41:46","guid":{"rendered":"\/blogs\/benbarefield\/archive\/2009\/07\/09\/winform-stuck-on-a-disconnected-monitor.aspx"},"modified":"2009-07-09T20:41:46","modified_gmt":"2009-07-09T20:41:46","slug":"winform-stuck-on-a-disconnected-monitor","status":"publish","type":"post","link":"https:\/\/www.benbarefield.com\/blog\/2009\/07\/09\/winform-stuck-on-a-disconnected-monitor\/","title":{"rendered":"WinForm stuck on a disconnected monitor"},"content":{"rendered":"<p>Recently, I had a customer report that the application I am writing got stuck off screen, and he couldn&#8217;t get at it. This happened by moving the application to his external monitor and then minimizing. After disconnecting the monitor, he would restore the application and it would be positioned off screen (on the now disconnected monitor).<\/p>\n<p>To solve this problem, I just look at all available screens and make sure that the location of the window is inside the working area of one of them. If it is not, I move it to the primary display:<\/p>\n<div style=\"border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px\">\n<div style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">bool<\/span> visible = <span style=\"color: #0000ff\">false<\/span>;<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">foreach<\/span> (var screen <span style=\"color: #0000ff\">in<\/span> Screen.AllScreens)<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">{<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">    <span style=\"color: #0000ff\">if<\/span> (screen.WorkingArea.Contains(<span style=\"color: #0000ff\">this<\/span>.DesktopLocation))<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">    {<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">        visible = <span style=\"color: #0000ff\">true<\/span>;<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">        <span style=\"color: #0000ff\">break<\/span>;<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">    }<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">}<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">if<\/span> (!visible)<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">    <span style=\"color: #0000ff\">this<\/span>.Location = <span style=\"color: #0000ff\">new<\/span> Point(100, 100);<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I had a customer report that the application I am writing got stuck off screen, and he couldn&#8217;t get at it. This happened by moving the application to his external monitor and then minimizing. After disconnecting the monitor, he &hellip; <a href=\"https:\/\/www.benbarefield.com\/blog\/2009\/07\/09\/winform-stuck-on-a-disconnected-monitor\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-winforms"],"_links":{"self":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":0,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}