简体   繁体   English

如何通过递归检查GroupBox中的控件(显示名称和类型)并在DataGridView上显示?

[英]How to check the controls (display name and type) in the GroupBox by recursion and display on a DataGridView?

How to read controller properties, automatically added to a table assigned to a DataGridView? 如何读取控制器属性,自动添加到分配给DataGridView的表中?

Dim dt As DataTable = New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("Type")
Dim n As Integer = Me.Controls.Count
For i As Integer = 0 To n - 1
    dt.Rows.Add(Me.Controls(i).Name.ToString, Me.Controls(i).GetType.ToString)
Next

DataGridView1.DataSource = dt

The above is the check for controls in the Form, it only displays Name and type of GroupBoxes, help me use recursive function to check the controls in GroupBox. 上面是对窗体中控件的检查,它只显示GroupBox的Nametype ,帮助我使用递归函数检查GroupBox中的控件。

Below is my idea, but it was not working: 以下是我的想法,但是没有用:

Public Sub V_gr(ByVal _Obj As Object)
    dt.Columns.Add("Name")
    dt.Columns.Add("Type")
    If (_Obj.Controls.count > 0) Then
        Dim i As Integer = _Obj.Controls.count - 1
        dt.Rows.Add(_Obj.Controls(i).Name.ToString, _Obj.Controls(i).GetType.ToString)
        DataGridView1.DataSource = dt
    End If
End Sub

Use a temporary table assigned to DataGridView and display the control information checked on it with 2 columns Name and Type 使用分配给DataGridView的临时表并使用2列NameType显示在其上检查的控件信息

You can split the DataTable creation and the Controls enumeration in two different methods: 您可以将DataTable的创建和Controls枚举拆分为两种不同的方法:

  • The first method is the public one, which can be called just passing the Parent control from which to start the enumeration. 第一种方法是公共方法,可以通过传递从其开始枚举的Parent控件来调用。
  • This method just creates a DataTable, then calls the private method to fill it with the results of the enumeration 此方法只是创建一个DataTable,然后调用private方法以将其枚举结果填充其中
  • The private method creates a new DataRow for each Control it finds and add it to the DataTable. private方法为找到的每个控件创建一个新的DataRow,并将其添加到DataTable中。

You could also modify the private method to return a List of objects that can be transformed to a DataTable after. 您还可以修改private方法以返回对象列表,该对象列表之后可以转换为DataTable。

I've added a Column, named "Parent" , which references the Parent of the control. 我添加了一个名为"Parent"的列,该列引用了控件的Parent。 It may be useful to know which are the Parents of these Controls. 了解哪些是这些控件的父级可能很有用。

' Find all Controls in the current Form
DataGridView1.DataSource = ControlsListToDataTable(Me)

Private Function ControlsListToDataTable(parent As Control) As DataTable
    If (parent Is Nothing) OrElse (Not parent.HasChildren) Then Return Nothing
    Dim dt As DataTable = New DataTable("ParentControls")
    dt.Columns.AddRange({
        New DataColumn() With {.ColumnName = "Name", .DataType = GetType(String)},
        New DataColumn() With {.ColumnName = "Type", .DataType = GetType(String)},
        New DataColumn() With {.ColumnName = "Parent", .DataType = GetType(String)}
    })
    GetAllControls(parent, dt)
    Return dt
End Function

Private Sub GetAllControls(parent As Control, dt As DataTable)
    For Each ctl As Control In parent.Controls.OfType(Of Control)
        dt.Rows.Add({ctl.Name, ctl.GetType().FullName, ctl.Parent.Name})
        If ctl.HasChildren Then GetAllControls(ctl, dt)
    Next
End Sub

To find a Control in the DataTable, you can use the DataTable.DefaultView Sort and FindRows methods: 要在数据表中找到控件,可以使用DataTable.DefaultView的Sort和FindRows方法:

[DataTable].DefaultView.Sort = "Name"
Dim result = [DataTable].DefaultView.FindRows("TextBox1")

Or use a LINQ method: 或使用LINQ方法:

Dim control = [DataTable].Rows.OfType(Of DataRow)().
              FirstOrDefault(Function(dr) dr(0).ToString().Equals("TextBox1"))

Where [DataTable] can be the original DataTable returned by the public method or the DataGridView.DataSource : 其中[DataTable]可以是公共方法或DataGridView.DataSource返回的原始DataTable:

 Dim dt = CType(DataGridView1.DataSource, DataTable)
 Dim control = dt.Rows.OfType(Of DataRow)(). (... etc ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM