Marek Zgadzaj

I use some of ApplicationSetting like Location and ClientSize. After I set the ClientSize the previous form size is never restored to its previous state after form is maximized.

I recorded this problem (1.5MB). The video can be downloaded from this address:

http://www.zgadzaj.pl/temp/ClientSize.wmv

In this video you can see that location and size is stored i restored properly but when a form is maximized then it losts its previous size. The restored size is wrong. The window's size in enlarged so it is bigger then the parent's surface. I think that, because of this issue, the ClientSize property is unusable.

I hope there is a workaround, I need an advice,

Marek



Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Muthu Krishnan. R

Can you plz put the code block here, so that I can check it out.






Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

nobugz

Check this thread.





Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Marek Zgadzaj

Muthu,

Thanks for trying to help me.,

Here is the project: http://www.zgadzaj.pl/temp/ClientSize.zip

Marek





Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Muthu Krishnan. R

Hi,

I';ve analyzed your problem.

It is working properly because

The Databinding update mode is OnPropertyChanged

So what is happening is when yo maximize the window the ClientSize property changed to the maximum values

and It is updated to ClientSize and It is updated to the binding source to.

Because of this when you restore the screen size the Maximum size is reloaded from the binding source.

You have to do some workaround for this in the ChildForm code.

Code Snippet

private Size mLastSize;

private FormWindowState mLastWinState;

private void ChildForm_Resize(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Normal)

if (mLastWinState == FormWindowState.Normal)

mLastSize = this.ClientSize;

else

{

this.ClientSize = mLastSize;

global::ClientSize.Properties.Settings.Default.ClientSize = mLastSize;

}

mLastWinState = this.WindowState;

}






Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Marek Zgadzaj

Hans,

nobugz wrote:
Check this thread.

I've tested the suggested code and it works, but partially. It cannot be used in a final application, the idea must be corrected in some way, sorry.

The child window is restored properly after it is maximized. If the window is closed in maximized state and next it is restored in its maximized state - so far so good, but from this point it is never reverted to its size which was before the maximization.

To see what I mean, please look at the video (1.5MB):

http://www.zgadzaj.pl/temp/ClientSize2.wmv

Here is the project:

http://www.zgadzaj.pl/temp/ClientSize2.zip

Marek





Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

nobugz

Change these event handlers:

private void Form1_Resize(object sender, EventArgs e)
{
if (mInitialized && this.WindowState != FormWindowState.Minimized)
{
if (this.WindowState == FormWindowState.Normal) Properties.Settings.Default.Size = this.Size;
Properties.Settings.Default.State = this.WindowState;
}
}

private void Form1_LocationChanged(object sender, EventArgs e)
{
if (mInitialized && this.WindowState == FormWindowState.Normal)
Properties.Settings.Default.Location = this.Location;
}






Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Marek Zgadzaj

Muthu,

Thank you for your advice. Your workaround is very compact but it does not resolve the problem. I mean the window's size is properly reverted to the normal state from its maximized state but canot be be restored when a window was closed in maximized state. Please read my earlier reply to Hans.

Please try these steps:

1. Open child window.

2. Maximize it.

3. Close child window.

4. Open child window again, it is opened in last maximized state.

5. Set the normal state - the window size is wrong.

Recently, I've found in MSDN an information about Control.ClientSize. Perhaps it helps to find a solution:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.clientsize.aspx

Marek





Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Muthu Krishnan. R

Check whether the following code solves your problem, I've enhanced the same code for you;

Code Snippet

private Size mLastSize;

private Point mLastLoc;

private FormWindowState mLastWinState;

private void ChildForm_SizeChanged(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Normal)

{

mLastLoc = this.Location;

if (mLastWinState == FormWindowState.Normal)

mLastSize = this.ClientSize;

else

{

this.ClientSize = mLastSize;

global::ClientSize.Properties.Settings.Default.ClientSize = mLastSize;

}

}

mLastWinState = this.WindowState;

}

private void ChildForm_Load(object sender, EventArgs e)

{

mLastLoc= global::ClientSize.Properties.Settings.Default.Location;

mLastSize=global::ClientSize.Properties.Settings.Default.ClientSize ;

}

private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)

{

global::ClientSize.Properties.Settings.Default.ClientSize = mLastSize;

global::ClientSize.Properties.Settings.Default.Location = mLastLoc;

}

private void ChildForm_LocationChanged(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Normal)

mLastLoc = global::ClientSize.Properties.Settings.Default.Location;

}






Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Marek Zgadzaj

Hans,

Yes!, that's it. It works perfectly. I fully understand the last changes.

Thank you for the help,

Marek





Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Marek Zgadzaj

Muthu,

Thank you very much. It works very good. There is a very small difference between your and Hans solution.

In Hans' idea the window is opened exactly in the same state as it was stored during close. So, it is opened maximized when it was closed in maximized state. In your solution, the window is opened in its last normal state even if it was maximized during closing. I think, that both solution are good because in some circumstances I prefere to open a window in its normal state.

Of course, that if we have a lot of windows, we need to modify a lot of code. Unfortunately, there are no automagically Application settings which could remember a window's state.

I would like to thank you both for spending so much time. Your advices are very valuable for me.

Marek





Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Marek Zgadzaj

Hans and Muthu,

Unfortunately this is not the end of story. After more tests I found another troubles. All are related to more than 1 child window are opened (MDI interface).

Muthu's solution

Open 1-st window in normal state. When the 2-nd and next windows are opened, they are invisible. We can find them only if we iconize (minimalize) this one which is available. The windows' captions are not aligned but they are partially covered one by one. Windows cannot be restored to theirs normal state. This problem does not occur in Hans code.

The same problem is when the 1-st window is maximized. The windows opened later are not visible.

Hans' solution

There is problem with parent's surface when 1-st window is maximized and then next window is opened. The background surface is destroyed (not repainted). When we move a window then the underlaying surfaxe is partially repainted,

In Hans solution there are no problems with minimalizing and restoring the windows.

Both solutions

There is a trouble with closing the application (parent window) in both solutions. After some time, ie. when several windows were opened, closed, maximized and minimized - the application cannot be closed. It remains open. It does not react on pressing close icon or shortcut Alt-F4. The only way is to use task manager and kill the application manually.

Marek





Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Muthu Krishnan. R

Hi,

It seems it is pretty much complex to use the DataBinding, Actually all forms are using you can open it using Ctrl+Tab, The problem is When one form moved, it changes the binding source, and eventually the binding source changes the other windows location. So all forms are located in the same location with same size.

I can give you worst one advice, better don't use binding here. Without binding it will work fine.

I've altered the code 'WITHOUT LOCATION & PROPERTY BINDING'

Code Snippet

private Size mLastSize;

private Point mLastLoc;

private FormWindowState mLastWinState;

private bool isInitialized = false;

private void ChildForm_SizeChanged(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Normal)

{

if (mLastWinState == FormWindowState.Normal)

{

mLastSize = this.ClientSize;

mLastLoc = this.Location;

Settings.Default.ClientSize = mLastSize;

Settings.Default.Location = mLastLoc;

}

else

{

this.ClientSize = mLastSize;

this.Location = mLastLoc;

Settings.Default.ClientSize = mLastSize;

Settings.Default.Location = mLastLoc;

}

}

mLastWinState = this.WindowState;

}

private void ChildForm_Load(object sender, EventArgs e)

{

mLastLoc= Settings.Default.Location;

this.Location = mLastLoc;

mLastSize=Settings.Default.ClientSize ;

this.ClientSize = mLastSize;

isInitialized = true;

}

private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)

{

Settings.Default.ClientSize = mLastSize;

Settings.Default.Location = mLastLoc;

}

private void ChildForm_LocationChanged(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Normal)

{

if (isInitialized == true)

{

mLastLoc = this.Location;

Settings.Default.Location = mLastLoc;

}

}

}






Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Marek Zgadzaj

Muthu,

I cannot find anything wrong. Yes, it works fine. Thank you for the help.

Marek





Re: Windows Forms General ApplicationSettings, ClientSize property does not revert form's previous size after maximizing

Marek Zgadzaj

Muthu,

I am here again, because I found an issue. As you remember I've written earlier that sometimes, after several operation open - close windows I could not close the whole application. I could'nt prove it but this problem occurs in

the last code.

I found exact steps to get it. Perhaps there is something wrong in my code so, I put the full source here:

http://www.zgadzaj.pl/temp/ClientSize1.zip

Try this steps:

1. Open 1-st child window and minimalize it.

2. Open 2-nd child window and minimalize it.

3. Close the last opened child window which is still in minimalized state.

Now, try to close the whole application. Application does not react. All windows can be closed but the aplication cannot be closed.

I hope to find the reason myself - but I would like you to know about it.

Marek