Docking and Anchoring on a Windows Form applicatio

2019-04-12 16:07发布

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.

8条回答
forever°为你锁心
2楼-- · 2019-04-12 16:35

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);
    }
查看更多
萌系小妹纸
3楼-- · 2019-04-12 16:37

You can get the required design by using nested panels along with few setting with Anchoring and Docking Properties.Follow the following steps: 1) Add the Form and put a Panel1 on it. Set its Dock Property as 'Fill' and ResizeMode as 'Grow&Shrink'. 2) Add Second panel2 and set its Dock Property to 'Bottom', Set the Height and set the Anchor property to 'Top,Left'. 3)Add Third panel and set its Dock Property to 'None', Set the Height and set the Anchor property to 'Top,Bottom,Left,Right'.

Save and Compile. Now all the panels Would maintain their relative Positioning With resizing.

查看更多
我命由我不由天
4楼-- · 2019-04-12 16:44

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.

查看更多
等我变得足够好
5楼-- · 2019-04-12 16:46

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.

查看更多
神经病院院长
6楼-- · 2019-04-12 16:49

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.

查看更多
三岁会撩人
7楼-- · 2019-04-12 16:51

I would like to add a point to @jasonh answer.

For the panel that occupies 2/3 of the form, you will have to set the AutoScroll property of the panel to true.

This will enable the panel to display scroll when the control size exceed the visibility to the user and also ensure the visibility of the smaller panel which is 1/3 of the forms height.

查看更多
登录 后发表回答