3
votes

I have a windows application with an MDI form and some child forms. I need to save the position of the child forms and then restore when it is opened again.

How to do that in C#?

Also, I need to check if the child form's title bar is not hidden under the mdi form (e.g title bar might get hidden because of mdi form's menus). If this is the case, then I need to set the position smartly.

Thanks in advance...

2

2 Answers

2
votes

Check out this CodeProject article: Restore Form Position and Size in C#

The gist of it is that you have to take the form's size and location and persist it in the project settings. You'll have to adapt it slightly to your needs (making sure the form fits within the boundaries of the parent window, etc.).

1
votes

You can use the .Bounds property of the form to get the position and size of the form. When the form is created, you set the .Bounds property and when the form is closing you can record the position and save it to the registry or a file. When setting the .Bounds property you need to make sure that the coordinates are valid for the current desktop size just in case the user has changed resolutions or something went wrong - you don't want the form being displayed off of the desktop. You can use Screen.PrimaryScreen.WorkingArea to get the current desktop dimensions and then compare the coordinates of the saved form bounds. You also need to set your form's StartPosition property to Manual if you want to manually set the location.