views:

36

answers:

3

I got many panels and one form. I have employed panels so I don't have to use multiple forms. But there is a problem with it. After I assign the panel size to the form size and show the form, the next time I assign it again, and to my surprise the panel gets smaller than the original. Why is this happening? Here is how I'm doing it.

this.Size = panelABC.Size;
panelABC.Dock = DockStyle.Fill;
panelABC.Visible = true;
A: 

Because the panel size has changed in relation to the control it is docked within by the time you come to call this code again. Using docking or anchoring will manage the panel size during resizing of parent containers.

For answer clarity - the actual answer is described in the comments of another posted answer.

Adam
How to remedy that?
Don't call the code again, or ignore the size setting when the panel is docked.
Adam
A: 

I would assume that there is some padding or other that means that after the first assignment some resizing needs to happen to fit things meaning that panelABC gets smaller (so it will fit correctly). Thus recursive calls make it keep getting smaller.

To confirm if this is the case stick in a breakpoint and check what your Sizes are as you go through...

Chris
A: 

What's this? If it's the form (or parent control) your panel lives in then that's your problem: docking your panel will make it slightly smaller than the parent. Subsequent calls to your code then make the parent match the panel's (smaller) size, and then you repeat the process...

Dan Puzey
'this' is the Form. So whats the solution?
Answering that with a question - why are you setting the form size to the panel size? Are you trying to achieve an effect? It is difficult to offer a fix, because no-one knows what you are trying to achieve with this sizing code.
Adam
Because I have multiple panels which show different form states (different windows you may say)
and each one's size is different
The form size should dictate the panel size if you dock the panel, so there is little point in sizing the form based on the panel. Also, as a side note, you can actually add forms as child controls to other controls - but they won't be allowed to be "top level" forms.
Adam
Sounds like you need to record the *initial* size of the panels, so when you call this code, don't use the panels *current* size, which is changing, use the stored initial size.
Adam
You mean hardcode them?
No, I mean on the first pass through for each panel, before you dock it, store its initial size. Next time through for that panel, use this stored size.
Adam
Thanks that is working perfectly.
@Adam - you should post your answer as a comment so that it can be accepted - you did more work than I did! ;-)
Dan Puzey