在 Windows 窗体中显示控件集合 [英] Displaying a collection of controls in Windows Forms

查看:31
本文介绍了在 Windows 窗体中显示控件集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示如下内容:-

每一行都与一个进程有关(PID、父级等信息).用户可以选中 checkbox 并单击 Launch 按钮以获取有关该过程的一些动态详细信息.

问题是 CheckedListBox 控件不允许超过一列,而 ListView 等其他控件(允许多列)不允许像 checkbox 嵌入到列中.

我想知道是否有一个控件可以让我拥有一个自定义控件列表,其中每个自定义控件包含一个 checkboxSome Text一些动态文本.

如何在 Windows 窗体中实现这一点?提前致谢.

解决方案

您可以使用以下任一选项:

  • DataGridView(

    示例 2 - 用户控件

    如果您需要自定义控件来托管更复杂的控件或对组件具有更多控制权或以与列不同的布局显示它们,您可以创建一个 UserControl 来托管您的组件,然后:

    • 如果您只想要自上而下的流程,请使用 Panel 并将您的用户控件添加到其中,并将控件的 Dock 属性设置为 Top.
    • 如果您可能需要自上而下以外的流程,请使用 FlowLayoutPanel 向其中添加控件实例.

    创建一个用户控件

    将它的实例添加到您的 PanelFlowLayoutPanel

    I want to display something like the following :-

    Each row is about a process (information like PID, Parent etc.). User can check the checkbox and click Launch button to get some dynamic details about that process.

    The problem is that CheckedListBox control doesn't allow more than one columns and other controls like ListView (which allow multiple columns) don't allow controls like checkbox to be embedded in a columns.

    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.

    How can this be achieved in Windows Forms? Thanks in advance.

    解决方案

    You can use either of these options:

    • DataGridView (Example)
      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 (Example)
      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

    这篇关于在 Windows 窗体中显示控件集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆