vb.net 循环遍历表单上的控件,如果 ListView 循环遍历列和行
vb.net Loop through Controls on Form and if ListView Loop through Columns and Rows
我在用户窗体中有 3 个 ListView,还有一些其他项目。目前,当我扩展 UserForm 时,ListViews 和其他项目会随着 UserForm 大小的增加而扩展。
我接下来要做的是通过循环遍历 UserForm 上的所有控件并检查它是否是 ListView,然后循环遍历所有列并根据需要扩展来扩展 ListView 中列的大小。
这是我目前所在的位置...
For Each Ctrl As Control In Me.Controls
If (TypeOf Ctrl Is ListView) Then
' This is where I'm not sure what to do!
' I want to loop through this Ctrl and view its columns
End If
Next
有人有什么想法吗?
如果 Plutonix 关于启用自动调整列大小的建议不起作用,请继续阅读...
要访问控件的属性,您需要先将其转换为 ListView
(到目前为止,您只检查了它是否 是 ListView
。然后,你可以遍历行和列:
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is ListView Then
Dim currentListView As ListView = DirectCast(Ctrl, ListView)
' Loop over the rows (items) in the view
For Each item As ListViewItem In currentListView.Items
Next
End If
Next
您最终可能还需要递归控件搜索——您的当前循环将只查找 Me
.
的直接子控件
另一个想法:只需 return 外部 for 循环中正确类型的控件:
For Each listViewControl As Control In Me.Controls.OfType(Of ListView)()
For Each item As ListViewItem In listViewControl.Items
Next
Next
我在用户窗体中有 3 个 ListView,还有一些其他项目。目前,当我扩展 UserForm 时,ListViews 和其他项目会随着 UserForm 大小的增加而扩展。
我接下来要做的是通过循环遍历 UserForm 上的所有控件并检查它是否是 ListView,然后循环遍历所有列并根据需要扩展来扩展 ListView 中列的大小。
这是我目前所在的位置...
For Each Ctrl As Control In Me.Controls
If (TypeOf Ctrl Is ListView) Then
' This is where I'm not sure what to do!
' I want to loop through this Ctrl and view its columns
End If
Next
有人有什么想法吗?
如果 Plutonix 关于启用自动调整列大小的建议不起作用,请继续阅读...
要访问控件的属性,您需要先将其转换为 ListView
(到目前为止,您只检查了它是否 是 ListView
。然后,你可以遍历行和列:
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is ListView Then
Dim currentListView As ListView = DirectCast(Ctrl, ListView)
' Loop over the rows (items) in the view
For Each item As ListViewItem In currentListView.Items
Next
End If
Next
您最终可能还需要递归控件搜索——您的当前循环将只查找 Me
.
另一个想法:只需 return 外部 for 循环中正确类型的控件:
For Each listViewControl As Control In Me.Controls.OfType(Of ListView)()
For Each item As ListViewItem In listViewControl.Items
Next
Next