views:

937

answers:

7

Hello!

I'm developing an app for Windows Mobile 5.0 and above, with C# and .NET Compact Framework 2.0 SP2.

I have a WinForm with two panels inside (upperPanel and bottomPanel). I want that upperPanel always fill 2/3 of form's height, and bottomPanel fills 1/3 of form's height. Both panels will fill completly form's width.

I've used this:

upperPanel.Dock = Fill;
bottomPanel.Dock = Bottom;

But upperPanel fills the form completly.

How can I do this? I want, more o less, the same gui on differents form factors and on landscape or protrait mode.

Thank you.

+1  A: 

Set both panels to "not anchored". That is: Remove Dock-Value and clear the Anchor property. Then, move the controls so they are sized the way you'd like them to be sized.

After that, upon resizing the form, they should resize relatively.

EDIT
Oops, just tried it and sure it doesn't work. I mixed this up with a solution that automatically keeps controls centered within the window...

Well, I'd guess you then have to create a handler for the form's Resize event and manually align the controls after the form has been resized.

Thorsten Dittmar
+1 for pointing out my blonde moment
GenericTypeTea
^^ no problem :)
Thorsten Dittmar
No, it doesn't work.
VansFannel
Sorry - edited my answer...
Thorsten Dittmar
While I can see that my first reply was clearly not helpful, the edit gives a valid hint to a solution. So to all those downvoting: please give a reason in the comments.
Thorsten Dittmar
Your answer it's a goog answer, but I was looking for a solution without doing Resize programmatically.
VansFannel
A: 

Go to Tools, Other Windows, Document Outline. Find the two panels, and swap the order of them. The control that has DockStyle.Fill has to come first for it to be docked correctly. (or last.. never sure which one it is, but it is one of them :p)

This won't solve the always 1/3 and 2/3 issue though... cause the bottom panel will have a fixed height (unless I am mistaken). I think maybe the TableLayoutPanel supports this though...

Update: As noted in the comments, that panel doesn't exist in the compact framework. So, I suppose the easiest solution to this problem would then try to use the docking, but update the height of the bottom panel whenever the size of the form changes.

Svish
It's a shame, but there is no TableLayoutPanel in the Compact Framework.
Thorsten Dittmar
BTW you are correct about the bottom panel having a fixed height.
Thorsten Dittmar
Ah, ok. Well, I've never used the Compact Framework, just regular WinForms.
Svish
A: 

Right click on the upperPanel and select Bring To Front. However, I don't think this will give you the result you want. When you resize, the bottom panel will remain the same height, while the upper panel will stretch to fill the form.

Using your docking settings, with this code might do the trick:

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);

        this.bottomPanel.Height = Convert.ToInt32((double)this.Height / 3.0);
    }
Philip Wallace
If you're going to downvote, have the courage to say why.
Philip Wallace
The result that I want it's on my solution. Try it before downvote.
VansFannel
A: 

My solution:

Place upperPanel on top-left corner and resize it to fill 2/3 of form's height and 100% of form's width.

Place bottomPanel on the space left by upperPanel.

Anchor upperPanel to all edges: top, left, bottom and right, and Dock it to none.

Dock bottomPanel to Bottom, and anchor it to Top and Left edges.

Thanks

VansFannel
You either dock or anchor - not both.
Philip Wallace
If you select a docking type, automatically it's select "Top, Left" on anchor property. Please, try the solution before saying something or adding negative points
VansFannel
If you dock, Anchor is ignored.
Philip Wallace
I don't think so. And now add me a new downvote.
VansFannel
From here:http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock.aspx"The Anchor and Dock properties are mutually exclusive. Only one can be set at a time, and the last one set takes precedence."
Philip Wallace
Therefore, your solution, which you accepted - is invalid.
Philip Wallace
This is very important for you. You are wasting a lot of time, and losing votes, to say that you are right and I am wrong. I don't know how to say the following in English, but try to transale it with google: "Estoy siendo sarcástico, no se si te has dado cuenta."
VansFannel
Not at all. What you was saying was wrong - I wanted you to know that, that's all. Case Closed.
Philip Wallace
Try the solution before saying something. It's works.
VansFannel
It seems the link I posted didn't sink in. Try this one:http://msdn.microsoft.com/es-es/library/system.windows.forms.control.dock.aspx
Philip Wallace
Las propiedades Anchor y Dock se excluyen mutuamente. Sólo se puede establecer una cada vez y la última establecida es la que tiene prioridad.
Philip Wallace
Try the solution before saying something. It's works.
VansFannel
Have you tried it?
VansFannel
Please check my comments, I never said it didn't work. All I said was that if you set the Anchor, you don't need to set the Dock - and vice-versa. Now please, get over it.
Philip Wallace
Ok, if it works... don't downvote!!!!
VansFannel
In your original question, you said you want the bottom panels height to be 1/3 of the form. With your solution, the bottom panels height does not change with the forms - it always stays the same as you resize the form.
Philip Wallace
As much as I want to avoid intruding on your argument I hasten to point out that Xaero is right. The two properties are mutually exclusive. If you dock, anchor is ignored. The fact that "it works" is much less important than knowing _why_ it works.
Quibblesome
+1  A: 

What you need to do is to put the bottom panel on first and set its Dock property to Bottom. Then set the panel's height to be 1/3 of the form's height. Finally, add a second panel and set its Dock property to Fill. The key here is that you want to add the control that will fill the remaining area to be added last. Alternatively, you can play around with the Bring to Front and Send to Back commands in Visual Studio to get the designer to cooperate.

You may also need to hook the OnSizeChanged event for the form and re-set the height of the bottom panel to account for layout changes. It's been a little while since I did compact framework programming, so I'm not sure.

jasonh
"The key here is that you want to add the control that will fill the remaining area to be added last" <-- that works perfectly for me. Thanks!
m3rLinEz
A: 

If you want this to work perfectly you'll need to add some code to the Resize event of the Form which then specifically works out the relative sizes and places the controls in the correct place after a resize.

If you're not worried about losing precision and the forms aren't going to move much you can avoid this by using some relatively smart anchoring. Essentially you're going to have to select a "grower" (the part of the form that gets bigger, the bigger the form gets). In this scenario I would probably anchor the top part to Top | Left | Right and the bottom part to Top | Left | Right | Bottom. This would mean that the lower part of the form will get bigger if the form is expanded. In most cases this is acceptable. If it isn't use the Resize event and some code.

Quibblesome
A: 

The easiest way to do this is to nest panels. Just set up panels for top bottom and fill. Then use panels within those panels to do the same. The only issues I've had therein are datagrid resizing, which is always a pain anyway. in that case, you have to use some code to resize the datagrid control on the form resize event.

Cole