tags:

views:

85

answers:

5

I create a bunch of forms, and I want to save and restore their position on application close/startup.

However, if the form is not visible, then the .top and .left are both 0. Only when it's visible are these properties populated with their 'real' values.

Right now my kludge is to show each form, save the info, then return it to its previous visible state:

        int i;
        bool formVisible;

        // Show all current forms and form positions in array frmTestPanels
        i = 0;
        while (frmTestPanels[i] != null)
        {
            formVisible = frmTestPanels[i].Visible;
            frmTestPanels[i].Visible = true;
            note(frmTestPanels[i].Text + "(" + frmTestPanels[i].Left.ToString() + ", " + frmTestPanels[i].Top.ToString() + ") visible: " + formVisible.ToString());
            frmTestPanels[i].Visible = formVisible;
            i++;
        }
        note(i.ToString() + " forms present");

note() is a simple function that just displays information.

This, of course, results in flashing all the non-visible forms on shut down (possibly on startup as well? Haven't gotten that far...) which is undesirable.

  • Is there another way to get the top and left of the form when it's not visible?
  • Alternately, is there a better way to save and restore form state?
A: 

What about saving the form location when the form is closed by the user, rather than when the application closes?

ageektrapped
The form is never closed by the user - these forms are created and persist for the life of the application. When the user "closes" a form, it merely sets the visible property to false. In some cases the forms are never shown - only when the user requests one.
Adam Davis
you can still save the position in the FormClosing event, even if it's not the user who closes it...
Thomas Levesque
So if the form.visible=false, I can read .top and .left when the form is closing? Even if this were the case, I will want to save the form state at times without closing the forms.
Adam Davis
A: 

Can't you just attach an event handler to each form's Move event and keep track of their locations whenever they move? Their position can only change when they're visible anyway but this way you get notified when they move. And then you can just dump your local cache of each Form's position to disk on shutdown.

I don't have a suitable idea for form creation, though, but I think it is possible to show them in a specified initial position. After all there is a Form.StartupPosition property. And Form.Location. According to the documentation, this should work.

Joey
Keeping track of this information separately is one option, though I was hoping to avoid handling that data separately.
Adam Davis
+1  A: 

You will need to trap the Closing and Minimizing events on the form, and store the position at that point in time.

These fields are not valid when the form is hidden or minimized.

Nick
So if I create the form as a hidden form, then at some point I must display it in order to get or set the coordinates?
Adam Davis
Yes. But it is probably logical to accept the default position the first time the user sees the form, and persist coordinates only once the form has been displayed to the user and then closed (or minimized).
Nick
+1  A: 

Whenever the user dismisses/hides/closes/makes invisible/whatevers a form, save its location. And only at this point in time. If the user is getting rid of a form, it must have been on the screen and you won't have to worry about it being not visible.

On the other side, don't create a form until the user asks for it for the first time. When each form is created, read its stored location and set it accordingly.

With this scheme if a form is never shown to the user, it's location will never be restored or saved.

NascarEd
For this particular application it's convenient to create them all at once, and it saves a trip to the database later. Further, the user perceives instant response when they request the form. Generally there aren't that many forms waiting around (5-20) so it's not a huge resource hog. So I'd prefer not creating them on request. But it increasingly sounds as though I'm going to have to keep track of their locations in another data structure.
Adam Davis
A: 

If the form is never seen by the user, Top and Left aren't really applicable, are they? Since they're related to the visual position, and there isn't a visual position...

Once the user has been shown the form at least once, you can capture the Top and Left into local variables before hiding it again, and use those local vars for storage of the position when the form is freed. Then you have an initial position for the next time the form is made visible.

Ken White