我正在尝试遍历我的 windows 窗体 VB.net 应用程序上的所有组合框。
我原以为这会奏效
Array.ForEach(Me.Controls.OfType(Of ComboBox).Items.Add(DataGridView1.Columns(i).Name)))
但我不能参考它似乎不知道它当时是一个组合的项目
我正在尝试获取所有组合框名称的列表,因此我希望可以在循环中使用该名称列表来添加项目并读取所选索引,但我的名称列表始终为空。我正在使用以下代码只是试图将列表发送到一个消息框以查看它是否正在抓取任何名称。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim allComboBoxValues As String = ""
Dim c As Control
Dim childc As Control
For Each c In Me.Controls
For Each childc In c.Controls
If TypeOf childc Is ComboBox Then
allComboBoxValues &= CType(childc, ComboBox).Text & ","
End If
Next
Next
MsgBox(allComboBoxValues)
If allComboBoxValues <> "" Then
MsgBox(allComboBoxValues)
End If
End Sub
波纹管function
可用于检索Controls
某种类型的所有子项。
Private Function GetAll(Control As Control, Type As Type) As IEnumerable(Of Control)
Dim Controls = Control.Controls.Cast(Of Control)()
Return Controls.SelectMany(Function(x) GetAll(x, Type)).Concat(Controls).Where(Function(y) y.GetType = Type)
End Function
用法:
GetAll(Me, GetType(Combobox))
满足您的需求:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim Values As String = String.Empty
For Each ComboBox As ComboBox In GetAll(Me, GetType(ComboBox))
Values &= ComboBox.Text & ","
Next
MsgBox(Values)
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句