Docking and Anchoring on a Windows Form application

13,788

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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);
    }
Share:
13,788
VansFannel
Author by

VansFannel

I'm software architect, entrepreneur and self-taught passionate with new technologies. At this moment I am studying a master's degree in advanced artificial intelligence and (in my free time ) I'm developing an immersive VR application with Unreal Engine. I have also interested in home automation applying what I'm learning with Udacity's nanodegree course in object and voice recognition.

Updated on August 07, 2022

Comments

  • VansFannel
    VansFannel almost 2 years

    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.

  • Thorsten Dittmar
    Thorsten Dittmar over 14 years
    It's a shame, but there is no TableLayoutPanel in the Compact Framework.
  • Thorsten Dittmar
    Thorsten Dittmar over 14 years
    BTW you are correct about the bottom panel having a fixed height.
  • Lemon
    Lemon over 14 years
    Ah, ok. Well, I've never used the Compact Framework, just regular WinForms.
  • Thorsten Dittmar
    Thorsten Dittmar over 14 years
    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.
  • VansFannel
    VansFannel over 14 years
    Your answer it's a goog answer, but I was looking for a solution without doing Resize programmatically.
  • VansFannel
    VansFannel over 14 years
    The result that I want it's on my solution. Try it before downvote.
  • Gant
    Gant about 14 years
    "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!
  • Dunc
    Dunc almost 11 years
    As you say, rather than adding last you can undock, bring to front, then re-dock... nice!