为多个组合框设置相同的SelectedIndex值 [英] Setting same SelectedIndex value for multiple combobox
问题描述
我有一个表格,其中有23个组合框
。为每个组合框编写 SelectedIndex = -1
肯定可以,但是我想知道是否还有其他方法可以按照下面的示例进行操作。 ?
I have a form where i have 23 Comboboxes
. Writing SelectedIndex=-1
for every combobox will definitely work but i want to know whether there's any other way of doing it as done in below given example. ?
For Each ctl As Control In (GroupBox1.Controls)
If TypeOf ctl Is TextBox Then
ctl.Text = ""
End If
Next
我尝试了此操作,
如果TypeOf ctl是ComboBox则
ctl.selectedIndex = -1
结束
但是它不起作用。
i tried this,
If TypeOf ctl Is ComboBox Then
ctl.selectedIndex=-1
End If
but it doesn't works. Please help me out.
推荐答案
在您的示例中,您的 ctl
变量为 Control
而不是 Combobox
,因此它没有 SelectIndex
属性-尽管您可以使用 DirectCast(ctl,Combobox)
抛弃它。
In your example your ctl
variable is a Control
and not a Combobox
so it does not have the SelectIndex
property - though you could have casted it back with DirectCast(ctl, Combobox)
.
For Each ctl As Control In (GroupBox1.Controls)
If TypeOf ctl Is Combobox Then
DirectCast(ctl, Combobox).SelectedIndex = -1
End If
Next
或者为您的循环创建一个特定于类型的控制循环。
Or create a loop of type specific control for your loop. No need to check type here.
Dim cbs = GroupBox1.Controls.OfType(Of Combobox)()
For Each cb In cbs
cb.SelectedIndex = -1
Next
这篇关于为多个组合框设置相同的SelectedIndex值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!