[英]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的
Name
和type
,帮助我使用递归函数检查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列
Name
和Type
显示在其上检查的控件信息
You can split the DataTable creation and the Controls enumeration in two different methods: 您可以将DataTable的创建和Controls枚举拆分为两种不同的方法:
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.