开发者

How do I make multiple controls in a Windows Form automatically resize with the window?

开发者 https://www.devze.com 2023-03-01 06:02 出处:网络
I\'m new to Windows Forms in Visual Studio, and I am wondering how to automaticly resize controls to the window size.

I'm new to Windows Forms in Visual Studio, and I am wondering how to automaticly resize controls to the window size.

Say, I have 2 controls in a panel, a List Box and a Button. I want the button to dock to the bottom, and I want the List Box to fit the rest of the sp开发者_如何学JAVAace. when the window resizes, the button should be at the bottom (as expected with docking), and the list box should stretch down to the button.

Is there a way to do this without any code?

Thanks.


Dock is pretty easy to use, but I recommend using the Anchor properties instead. Resize your form to a reasonable size in the Designer. Then, place your controls to look the way you want. Then, decide which controls should resize with the form and set the Anchor property as follows:

  • If you want the control to resize with the form in width, set the Right anchor.
  • If you want to resize height, set the Bottom anchor.
  • If you want the control to stay right when the form resizes, unset the Left anchor.
  • If you want the control to stay bottom when the form resizes, unset the Top anchor.

The problem I have with Docks is that they sometimes act funny when controls are not declared in a specific order, and to get the effect you want, sometimes you have to create extraneous panels just to hold controls.


It really gets messy when you want to maintain the aspect ratio of each control. One way, which is not really up to the mark if you want to get into fixing the details, is to use TableLayoutPanel and use Dock and Anchor wisely to achieve what you want.

How do I make multiple controls in a Windows Form automatically resize with the window?


Use the dock and fill options on the controls. Look under properties for each object, and containers if they are in any.


You can use SplitContainer

Google for examples. Here is one


Try setting your ListBox's Dock property to Fill.

You'll need to watch for one thing though: by default the ListBox will size itself to display whole list items. If you resize the control so that it displays a partial item it will adjust itself so it will display a complete item. This can make the control appear to lose its 'Dock'ing behavior. The solution for this is to set the ListBox's IntegralHeight property to false, which specifies that the control not resize itself to fit complete items.

0

精彩评论

暂无评论...
验证码 换一张
取 消