Displaying a collection of controls in Windows Forms

风格不统一 提交于 2019-11-27 16:22:47

You can use either of these options:

  • DataGridView
    You can use DataGridView to show multiple columns of different types, including TextBox, Label, CheckBox, ComboBox, Image, Button, Link. You also can customize appearance of the grid by custom painting or adding new custom column types.
  • UserControl
    You can create a composite control or UserControl containing any other controls which you need and use it as a row template, then you can show all rows by hosting multiple instance of that user control in a Panel or FlowLayoutPanel.
  • TableLayoutPanel
    You can use a TableLayoutPanel containing multiple columns and rows. Each cell of TableLayoutPanel can host a control.

  • DataRepeater
    You can use a DataRepeater control to create a row template and show a list of rows using that template.

Example 1 - DatGridView

If you want to use data binding and show specific controls including TextBox, Label, CheckBox, ComboBox, Image, Button, Link a row, DataGridView is great. It's customize-able and you can add some other different column types or customize painting of the grid or benefit from wide range of useful events for validating and so on.

In following image you can see a DataGridView with RowHeaderVisible and ColumnHeaderVisible set to false to act like a List of fields without header:

Example 2 - UserControl

If you need custom control to host more complicated controls or having more control on components or show them in a different layout than columns, you can create a UserControl hosting your components, then:

  • If you only want top-down flow, Use a Panel and add your user control to it with Dock property of control set to Top.
  • If you may want flows other than top-down Use a FlowLayoutPanel to add instances of your control to it.

Create a UserControl

Add instances of it to your Panel or FlowLayoutPanel

You could use the TableLayoutPanel container.

I want to know if there is a control which will allow me to have a list of custom controls where each custom control contains a checkbox, Some Text and Some Dynamic Text.

One option could be that you create the following as a separate user control,

...and as container control use container like FlowLayoutPanel and keep adding the user control into the FlowLayoutPanel.

Make sure that the direction of FlowLayoutPanel is set TopDown

this.FlowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!