Changing the location of a form through code
-
Similar to my last thread, I'd like to save the form location as well. In talking to another programmer, he said that it'd probably be a good idea to have some preventative measures (i.e.: making sure the program doesn't appear off-screen). Is there a good or bad way to do this?
-
Similar to my last thread, I'd like to save the form location as well. In talking to another programmer, he said that it'd probably be a good idea to have some preventative measures (i.e.: making sure the program doesn't appear off-screen). Is there a good or bad way to do this?
Hi again, Your earlier post was on size, and you can protect size with the Form.MinimumSize property (it will refuse a Size that is less than that); just give it a reasonable minimum with Visual Designer. You could also set a MaximumSize, but there is no need AFAIK. As far as location is concerned, if Form.StartPosition is not set to Manual, you have no control; and if it is, you are responsible; bad coordinates will be accepted but may put it off-screen. So you may want to validate them, by comparing with (0,0) and SystemInformation.PrimaryMonitorSize, assuming you only have one monitor. With multiple monitors it gets complex easily. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Hi again, Your earlier post was on size, and you can protect size with the Form.MinimumSize property (it will refuse a Size that is less than that); just give it a reasonable minimum with Visual Designer. You could also set a MaximumSize, but there is no need AFAIK. As far as location is concerned, if Form.StartPosition is not set to Manual, you have no control; and if it is, you are responsible; bad coordinates will be accepted but may put it off-screen. So you may want to validate them, by comparing with (0,0) and SystemInformation.PrimaryMonitorSize, assuming you only have one monitor. With multiple monitors it gets complex easily. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Unfortunately, 95% of the people that will be using this program have 2 or more monitors (yes some have 3 and 4). It may be in my best interests to leave location alone and let Windows handle that. Someday I'll figure it out...
well you can save the location as a point value in your settings.
my.settings.SaveLocation = my.settings.location
where SaveLocation is your variable.Please check out my articles: The ANZAC's articles
-
Unfortunately, 95% of the people that will be using this program have 2 or more monitors (yes some have 3 and 4). It may be in my best interests to leave location alone and let Windows handle that. Someday I'll figure it out...
IIRC on a multi-monitor environment, Windows puts every Form on the "primary monitor" by default, and that might not be what you want. If the desktop got extended over all monitors (that's a checkbox per monitor), and if the monitors are arranged such that they (almost) cover a true rectangle, then it makes sense to validate a form's bounds (thats location+size) against SystemInformation.VirtualScreen You may want to have a closer look at the Rectangle type, in particular Rectangle.Contains(). If the desktop does not extend over all monitors, then I don't know how forms get positioned on those non-desktop monitors. Feel free to tell me more about that. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google