質問

I am trying to build an user control in C#/.NET 4.0/WinForms that would contain two (or more) GroupBox elements placed above each other, first one touching the top border of my user control while last one touching the bottom line.

I would also want the height of these GroupBox elements to be all the same, and on top of that (now this seems to be the difficult part), all of it has to work flawlessly when user changes the height of the application window. So far I have not been able to find any combination of the Anchor property that would accomplish just this so I am starting to think that writing a custom resize handler would be the only option, but before I delve into such (in my eyes) dirty solution, I was wondering if there is some easy way to create user control that would fit my description?

Here is an illustration of what I want to accomplish, here is what happens if top GroupBox has Top and Left Anchor and bottom one has Top, Bottom and Left and the application window gets resized slightly (bottom one takes all the extra space instead of both GroupBoxes sharing it equally) and finally, here is what happens if I use the properties from previous example but add a Bottom Anchor to the upper GroupBox.

Thanks in advance!

役に立ちましたか?

解決

You can let your container do some of the work, i.e. use a TableLayoutPanel as the container set it for 2 rows and 1 column and set the Dock Property to Fill, you then can add your GroupBoxes to each of the cells of the TableLayoutPanel setting their Dock Mode to Fill also, you probably will have set the position of any child controls in the Groupbox because if you set apposing anchors it will affect the size of the control. Play around with it and see if it works for you.

他のヒント

It is not at all a dirty solution to add code in to the SizeChanged event handler to ensure layout elements appear as they should - it is something that every WinFroms UI developer will have to do at some point!

The solution is simple, set your anchors according to your resizing model and the other surrounding UI controls, then simply get the Height property of the control, divide it by 2, remove a figure to account for the margins and then assign the resultant figure to the height property of each GroupBox.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top