views:

83

answers:

4

Hello, I'm converting an app from ASP.NET WebForms to WinForms. There is one asp.net page which contains a ListView/Repeater that contains several custom controls, which in turn contain a ListView with other custom controls. Basically the layout looks like a TreeView, but on each node/leaf there are few controls like comboboxes, etc.

When this is in ASP.NET, the page automatically lays itself out, so it is several screens tall - if I add 20 buttons into a Panel, it will grow and the browser will get scrollbars.

I'd like to do the same thing in a WinForms application - so I'll have a user control that will contain a lot of controls in a some variation of Panel (Flow, Table layout), and the controls might have another controls inside them, etc.

The problem is, that when I make winforms app, each control has specific height in the design time. I'd like some user controls to be able to grow with their contents - so they'll add up. In the main Form, there should be a vertical scrollbar, just like in the web browser when the generated page is taller than the screen.

I'd just like to get some general pointers in the right direction. Thanks.

+1  A: 

Use Anchor and Dock container properties.

serhio
A: 

Anchor the controls to the parent. Anchoring all four sides will cause it to stretch.

nonnb
A: 

Yes, to expound on Anchor and Dock...try this

-Place a Panel on an empty form, and set its dock property to Top

-place a textbox in the panel, and Dock it to Full...it should fill the whole top panel

-Place a splitter on the form, and if not already docked correctly, set its dock to top

-place another panel below the splitter, and set its Dock to Fill

-place another textbox inside the lower panel and fill it as as well

Now you have a form with two resiable textboxes and will resize when the form does.

*you may have to set the textbox MultiLine property to true but not sure.

Hope this helps.

Jeremy
A: 

If the Anchoring and Docking answers don't work for you, there is another option. It's not pretty, but you can access a control's properties and change them dynamically during runtime. You'd do something like: if(listBox.Items.Count > [yourVal]) listBox.height = [yourFormula] or something.

It's been a while since I've done a Win Form (and I don't have my IDE fired up at the moment) but I'm pretty sure there's even a ScrollPanel or other scrolling control that you can set on your form.

That said, when you're working with WinForms, the less scrolling you can make your users do, the better.

AllenG