Screen Size Issues

Using VB.Net 2010 (Pro)

I have an interesting issue that has cropped up.  

In my application, when the app shuts down (in the main form's Form_Closing event), I save the position of the form.  The next time the app starts, the setting are read and the form is (supposed to be) returned to it's last location.

However, occasionally, I will get a report that someone has started the app, but the form is not showing.  The task bar button is visible, but it seemingly does nothing when it's clicked.  After pulling on my hair for a few weeks, I finally looked at the settings XML file and saw that the top and left positions were being saved as -32000 for each.  Once I deleted the Top and Left settings, the app worked again no prob.

So what might cause this?

A couple of points that may be significant...
1. This is intermittent.
2. The engineers who use my app all have dual-monitor set-ups
LVL 10
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Can you post the code you are using to get the apps positions? And also to restore?

Are you getting the virtual screen size and then position for example?
ClifAuthor Commented:
I am getting the app (form, actually) position with this code:
With frmMain
    nTop = .Top
    nLeft = .Left
End With

clsSettings.SetSetting("MainFormTop", nTop.ToString(), sXMLFilename)
clsSettings.SetSetting("MainFormLeft", nLeft.ToString(), sXMLFilename)

Open in new window

And I retrieve it this way:
nTop = Val(clsSettings.GetSetting("MainFormTop", sXMLFilename))
nLeft = Val(clsSettings.GetSetting("MainFormLeft", sXMLFilename))

With frmMain
    .Left = nLeft
    .Top = nTop
End With

Open in new window

Pretty straight forward, actually
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Why "frmMain" and not "Me".  Where is this code running from?  It's possible that you are not referencing the correct instance of your form.

How does your app start?  Using the default "Startup Object"?...or have you modded it to start from Sub Main()?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

ClifAuthor Commented:
The code is running from a module.  The code is actually part of a subroutine where the form is passed in.

The app starts using the main form (whatever the default was, it's working fine).  To tell you the truth, I can't find where I would set it up to start with Sub Main()
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
So "frmMain" is the name of the local variable in the Module?  

Why not just run that code directly in the FormClosing() event?...and the Load() event when it opens.
Jacques Bourgeois (James Burger)PresidentCommented:
It is usually possible to get negative values for those settings when the form is moved so that that is upper left corner is outside of the screen. But a value of 32000 is to huge for that, unless you have a form that is 32000 pixels wide. Very nice screens you would have :-)

It could be easily be done through code however.

Could be some artefact of the dual monitor driver for you graphic card.

Is the value always 32000? If so, I would look in the code or something wrong with the driver.

If you do not find the source of the problem, simply check for the condition when retrieving the values and adjust accordingly:
nTop = Val(clsSettings.GetSetting("MainFormTop", sXMLFilename))
nLeft = Val(clsSettings.GetSetting("MainFormLeft", sXMLFilename))

if nTop < 0 then nTop=0
if nLeft < 0 then nLeft=0

With frmMain
    .Left = nLeft
    .Top = nTop
End With

Open in new window

And it probably does not have anything to do with your questions, but why don't you use the standard settings mechanism built into Visual Studio instead of using your own. It would save the position as a Point object, not Strings that you need to convert back and forth between Integer ans String.

If multiple users are working on the same computer, it would also automatically save a unique settings for each individual user.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ClifAuthor Commented:
Code Reuse.

I have a bunch of commonly called procedures in a module.  That way, whenever I write a new app, I don't have to retype all the code (potentially forgetting something, or getting the wrong stuff in the wrong place).

The procedure that saves the Top/Left also has a lot more stuff from the app that it's saving (previous worked on documents, etc).
ClifAuthor Commented:
Thanks for the code snippet.  My previous post was actually addressed to Idle_Mind.
Hello, Clif,

Restoring the form position to it's previous location can also cause a problem when the user moves from a computer with a larger screen to one with a smaller screen, or possibly in the case of your users, to a system with only one screen.  

I would recommend that, when restoring the position, you compare the position with the size (and number) of the user's current screen(s).  Then if necessary, reset the form position to ensure it appears within the current screen.  I guess this would also handle the current intermittent problem.

Jacques Bourgeois (James Burger)PresidentCommented:
@omegaomega and Clif

Omega is right, and that would be one more reason to use the settings system built into the framework. Since it records the settings at the user level on each computer, moving the project around would not be a problem, each environment would have its own set of settings.
ClifAuthor Commented:
The app, and as such the settings, are limited to one computer.  That is, no one copies the settings file to another computer.

As far as dropping a screen, I suppose it is possible, since some of the engineers are running laptops with a second monitor, but the one I did notice the bad settings on was a desktop model.
ClifAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.