4

I have a form in C# (WinForm). It looks like this:


(LOGO)

blank space for labels that I add through code (I can fit 10 labels in this space)

(close button)


The blank space can hold about 10 labels.

I am stumped on how I would make this form scrollable if I want to add 20 labels? If I add 20 labels via code, then the 11th label will overlap with my close button and the 12th+ label(s) will run off the end of the form.

How do I make just the blank space portion of my form scrollable where I am creating the labels? I don't want to use a listbox.

Thanks.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
fraXis
  • 3,141
  • 8
  • 44
  • 53

4 Answers4

4

You should try using either a TableLayoutPanel or a FlowLayoutPanel as a container for your Label controls.

A TableLayoutPanel will allow you a finer level of control over where your labels are positioned. Like an HTML table, you specify the exact cell position (using row and column coordinates) of each control.

By contrast, a FlowLayoutPanel will handle the positioning of its contents automatically, either in a vertical or horizontal layout configuration. The positioning is determined by the order in which you add the controls, allowing you to achieve a dynamic layout with a minimal amount of fuss.

Either will allow you to add your label controls to it at run-time and size itself appropriately. In order for layout panel to be scrollable, make sure that you set its AutoScroll property to "True".

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
2

Maybe a FlowLayoutPanel with AutoScroll set to true and FlowDirection set to TopDown.

Chad
  • 3,159
  • 4
  • 33
  • 43
1

Place all controls inside a panel and use scrollbar control.

Understand .NET Scrollbars

A G
  • 21,087
  • 11
  • 87
  • 112
  • 1
    Given the existence of the `AutoScroll` property on most of the container controls offered by WinForms, this is probably the worst possible idea, not to mention the most difficult. – Cody Gray - on strike Dec 13 '10 at 10:55
  • With auto scroll the logo and the close button would also scroll. Not a good UI design by any standards. – A G Dec 13 '10 at 10:58
  • 2
    Only if you placed the logo and close button *inside* of the panel. You could keep them outside, dock the panel control in between them, and only add the label controls to the panel. – Cody Gray - on strike Dec 13 '10 at 11:00
  • Also with scrollbars you get freedom of positioning the controls. Both tablelayout and flowlayouts are quite constraint and hard to manage as the design gets complex. – A G Dec 13 '10 at 11:13
1

You could use a FlowLayoutPanel.

Add as many labels you need and enable AutoScroll on the FlowLayoutPanel.

Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87