我正在尝试迭代一组ComboBox并使用连接字符串&设置属性。变量来表示控件的名称。但是,我不能让表单的实例将(String& Integer_Variable)识别为其控件之一 - 因此它不会将任何适当的属性或子例程识别为System.Windows的成员。 Forms.Control。
我找到了DirectCast solution on SO它似乎有用(虽然我很怀疑),但这感觉就像一个非常笨拙的解决方案。有更清洁的方法吗?
For myTempCount = 1 To 6
If tempValue < Me.("ComboBox" & myTempCount).Items.Count Then
ComboBox.SelectedIndex = tempValue 'appears to work -- how?
Me.ComboBox.SelectedIndex = tempValue 'appears to work
Me.("ComboBox" & myTempCount).SelectedIndex = tempValue 'doesn't work
Me.Controls.("ComboBox" & myTempCount).SelectedIndex = tempValue 'doesn't work
DirectCast(Me.Controls.Find(("ComboBox" & myTempCount), True)(0), ComboBox).SelectedIndex = tempValue 'appears to work
DirectCast(Me.Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue 'appears to work
Next
这段代码最初是VBA / VB6,我通过ArtinSoft的Visual Basic Upgrade Companion(VBUC)进行了介绍。 FWIW,我正在使用Microsoft Visual Basic 2010 Express。
答案 0 :(得分:3)
回答你的问题:
ComboBox1.SelectedIndex
有效,因为ComboBox1是Form的ControlCollection中存在的控件Me.ComboBoxPrinter1.SelectedIndex
有效,因为我是对您的Form类的引用,它引用了Control。Me.("ComboBoxPrinter" & myTempCount).SelectedIndex
不起作用,因为字符串ComboBoxPrinter & myTempCount
是字符串而不是控件。Me.Controls.("ComboBoxPrinter" & myTempCount).SelectedIndex
出于同样的原因无效。我个人通常使用DirectCast以外的CType。 CType和DirectCast之间根据此link的主要区别在于DirectCast必须是精确类型,因为CType可用于缩小或扩大转换。尽管更挑剔,DirectCast更有效率。
据说你可以这样做:
For myTempCount = 1 To 6
If Controls.ContainsKey("ComboBox" & myTempCount) Then
CType(Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue
End If
Next
我没有在控件前面使用Me,因为它指的是同一个集合,如果你的控件在另一个集合中,你将需要使用该Container。即如果您使用的是专家组Panel1.Controls.ContainsKey
答案 1 :(得分:0)
哎哟!!!我曾经和Direct Cast搞过一次。我记得这是一场噩梦。我倾向于坚持使用服务器端控件,或者将它们作为客户端Javascript / Ajax编写。你上面的代码在哪里失败了?任何内部例外?
答案 2 :(得分:0)
也许你可以尝试这样的事情(C#):
List<Control> comboBoxes = new List<Control>
{
ComboBoxPrinter1,
ComboBoxPrinter2,
ComboBoxPrinter3,
ComboBoxPrinter4,
ComboBoxPrinter5,
ComboBoxPrinter6
};
// loop through combo boxes collection by position
for (int = 0; i < comboBoxes.Length;i++)
{
// put in your if logic here, and refer to current combo box using comboBoxes[i]
}
以上是使用在线工具转换为VB.NET的代码:
Dim comboBoxes As New List(Of Control)() From { _ ComboBoxPrinter1, _ ComboBoxPrinter2, _ ComboBoxPrinter3, _ ComboBoxPrinter4, _ ComboBoxPrinter5, _ ComboBoxPrinter6 _ } For i As Integer = 0 To comboBoxes.Count - 1 // you can hook in your if logic and refer to each combo box using comboBoxes[i] Next
我希望这有帮助!
答案 3 :(得分:0)
我再次遇到了这个问题,不同类型的多个控件需要在公共属性上执行相同的操作(如.Text
)。由于您无法使用变量来表示CType()
中的控件类型参数,因此您必须使用条件和相应的硬编码CType()
命令来获取控件。这就是我想出的:
Function getControl(ByVal controlName As String) As Control
numCtrls = FrameMain.Controls.Count()
For I As Integer = 0 To numCtrls - 1
If FrameMain.Controls.Item(I).Name = controlName Then
If TypeOf FrameMain.Controls.Item(I) Is ComboBox Then
Return CType(FrameMain.Controls(controlName), ComboBox)
End If
End If
Next
End Function
controlName
是串联的字符串名称。因此,您可以使用此函数,与之前使用的答案CType()
完全相同:
getControl("TextBox" & myTempCount).Text = "whatever"
答案 4 :(得分:0)
这样的事情:
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim control As Control = Me.Controls("Button1")
Select Case control.GetType
Case GetType(Button)
Dim btn As Button = DirectCast(control, Button)
With btn
.Text = "hi"
End With
Case GetType(Label)
Dim lbl As Label = DirectCast(control, Label)
With lbl
.Text = "hi"
End With
Case Else 'etc
End Select
End Sub
答案 5 :(得分:0)
Sub Button2Click(sender As Object, e As EventArgs)
'对于 i = 1 到 5
如果 textBox15_08_St.Text = "" 那么
MessageBox.Show("Bitte die Anzahl eintragen!","Info",MessageBoxButtons.OK,MessageBoxIcon.Information)
退出子
如果结束
If dataGridView15_08.SelectedRows.Count = 0 And dataGridView15_08.SelectedCells.Count = 0 Then
MessageBox.Show("Bitte eine Zeile auswählen","Info",MessageBoxButtons.OK,MessageBoxIcon.Information)
Exit Sub
End If
If dataGridView15_08.SelectedRows.Count > 1 Then
MessageBox.Show("Bitte nur 1 Zeile auswählen!","Info",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
Exit Sub
End If
If dataGridView15_08.Rows.Count = 0 Then
MessageBox.Show("Bitte Filter überprüfen!","Info",MessageBoxButtons.OK,MessageBoxIcon.Information)
Exit Sub
End If
Dim i As Integer = 1
For Each c As CheckBox In panelCheckBox.Controls
Dim BoxName As String = "checkBox15_08_" & Str(i) Dim CheckName As String = "checkBox15_08_" & Str(i)
BoxName = BoxName.Replace(" ", "")
If c.Name = BoxName Then
If c.Checked = False Then
c.Checked = True
CType(Me.panelCheckBox.Controls(CheckName.Replace(" ","")), CheckBox).Enabled = True
CType(Me.panelTextbox.Controls(BoxName.Replace("checkBox15_08","Hersteller15")), TextBox).Text = Convert.ToString(dataGridView15_08.Rows(dataGridView15_08.CurrentRow.Index).Cells(1).Value)
CType(Me.panelTextbox.Controls(BoxName.Replace("checkBox15_08","Artikel15")), TextBox).Text = Convert.ToString(dataGridView15_08.Rows(dataGridView15_08.CurrentRow.Index).Cells(3).Value)
CType(Me.panelTextbox.Controls(BoxName.Replace("checkBox15_08","Artikel_St_15")), TextBox).Text = Me.textBox15_08_St.Text
textBox15_08_St.Text = ""
Exit For
Else
If CType(Me.panelTextbox.Controls(BoxName.Replace("checkBox15_08","Hersteller15")), TextBox).Text = Convert.ToString(dataGridView15_08.Rows(dataGridView15_08.CurrentRow.Index).Cells(1).Value) _
And CType(Me.panelTextbox.Controls(BoxName.Replace("checkBox15_08","Artikel15")), TextBox).Text = Convert.ToString(dataGridView15_08.Rows(dataGridView15_08.CurrentRow.Index).Cells(3).Value) Then
MessageBox.Show("Dieser Artikel wurde bereits hinzugefügt","Info",MessageBoxButtons.OK,MessageBoxIcon.Information)
Exit For
End If
End If
End If
i = i+1
If i = 31 Then
MessageBox.Show("Die maximale Anzahl wurde erreicht" & vbCrLf & "Bitte setze Dich mit dem Programierer in Verbindung" & vbCrLf & "Um ein Update Erweiterung zu planen","Info",MessageBoxButtons.OK,MessageBoxIcon.Error)
Exit For
End If
下一步
'结束于 结束子
Sub checkBox15_08_1Click(sender As Object, e As EventArgs) 处理 checkBox15_08_1.Click checkBox15_08_1.Checked = Me.getControl(1) 结束子
Sub checkBox15_08_2Click(sender As Object, e As EventArgs) 处理 checkBox15_08_2.Click checkBox15_08_2.Checked = Me.getControl(2) 结束子
Sub checkBox15_08_3Click(sender As Object, e As EventArgs) 处理 checkBox15_08_3.Click checkBox15_08_3.Checked = Me.getControl(3) 结束子
Sub checkBox15_08_4Click(sender As Object, e As EventArgs) 处理 checkBox15_08_4.Click checkBox15_08_4.Checked = Me.getControl(4) 结束子
Sub checkBox15_08_5Click(sender As Object, e As EventArgs) 处理 checkBox15_08_5.Click checkBox15_08_5.Checked = Me.getControl(5) 结束子
Sub checkBox15_08_6Click(sender As Object, e As EventArgs) 处理 checkBox15_08_6.Click checkBox15_08_6.Checked = Me.getControl(6) 结束子
Sub checkBox15_08_7Click(sender As Object, e As EventArgs) 处理 checkBox15_08_7.Click checkBox15_08_7.Checked = Me.getControl(7) 结束子
Sub checkBox15_08_8Click(sender As Object, e As EventArgs) 处理 checkBox15_08_8.Click checkBox15_08_8.Checked = Me.getControl(8) 结束子
Sub checkBox15_08_9Click(sender As Object, e As EventArgs) 处理 checkBox15_08_9.Click checkBox15_08_9.Checked = Me.getControl(9) 结束子
Sub checkBox15_08_10Click(sender As Object, e As EventArgs) 处理 checkBox15_08_10.Click checkBox15_08_10.Checked = Me.getControl(10) 结束子
Sub checkBox15_08_11Click(sender As Object, e As EventArgs) 处理 checkBox15_08_11.Click checkBox15_08_11.Checked = Me.getControl(11) 结束子
Sub checkBox15_08_12Click(sender As Object, e As EventArgs) 处理 checkBox15_08_12.Click checkBox15_08_12.Checked = Me.getControl(12) 结束子
Sub checkBox15_08_13Click(sender As Object, e As EventArgs) 处理 checkBox15_08_13.Click checkBox15_08_13.Checked = Me.getControl(13) 结束子
Sub checkBox15_08_14Click(sender As Object, e As EventArgs) 处理 checkBox15_08_14.Click checkBox15_08_14.Checked = Me.getControl(14) 结束子
Sub checkBox15_08_15Click(sender As Object, e As EventArgs) 处理 checkBox15_08_15.Click checkBox15_08_15.Checked = Me.getControl(15) 结束子
Sub checkBox15_08_16Click(sender As Object, e As EventArgs) 处理 checkBox15_08_16.Click checkBox15_08_16.Checked = Me.getControl(16) 结束子
Sub checkBox15_08_17Click(sender As Object, e As EventArgs) 处理 checkBox15_08_17.Click checkBox15_08_17.Checked = Me.getControl(17) 结束子
Sub checkBox15_08_18Click(sender As Object, e As EventArgs) 处理 checkBox15_08_18.Click checkBox15_08_18.Checked = Me.getControl(18) 结束子
Sub checkBox15_08_19Click(sender As Object, e As EventArgs) 处理 checkBox15_08_19.Click checkBox15_08_19.Checked = Me.getControl(19) 结束子
Sub checkBox15_08_20Click(sender As Object, e As EventArgs) 处理 checkBox15_08_20.Click checkBox15_08_20.Checked = Me.getControl(20) 结束子
Sub checkBox15_08_21Click(sender As Object, e As EventArgs) 处理 checkBox15_08_21.Click checkBox15_08_21.Checked = Me.getControl(21) 结束子
Sub checkBox15_08_22Click(sender As Object, e As EventArgs) 处理 checkBox15_08_22.Click checkBox15_08_22.Checked = Me.getControl(22) 结束子
Sub checkBox15_08_23Click(sender As Object, e As EventArgs) 处理 checkBox15_08_23.Click checkBox15_08_23.Checked = Me.getControl(23) 结束子 子 checkBox15_08_24Click(sender As Object, e As EventArgs) 处理 checkBox15_08_24.Click checkBox15_08_24.Checked = Me.getControl(24) 结束子
Sub checkBox15_08_25Click(sender As Object, e As EventArgs) 处理 checkBox15_08_25.Click checkBox15_08_25.Checked = Me.getControl(24) 结束子
Sub checkBox15_08_26Click(sender As Object, e As EventArgs) 处理 checkBox15_08_26.Click checkBox15_08_26.Checked = Me.getControl(26) 结束子 Sub checkBox15_08_27Click(sender As Object, e As EventArgs) 处理 checkBox15_08_27.Click checkBox15_08_27.Checked = Me.getControl(27) 结束子
Sub checkBox15_08_28Click(sender As Object, e As EventArgs) 处理 checkBox15_08_28.Click checkBox15_08_28.Checked = Me.getControl(28) 结束子
Sub checkBox15_08_29Click(sender As Object, e As EventArgs) 处理 checkBox15_08_29.Click checkBox15_08_29.Checked = Me.getControl(29) 结束子
Sub checkBox15_08_30Click(sender As Object, e As EventArgs) 处理 checkBox15_08_30.Click checkBox15_08_30.Checked = Me.getControl(30) 结束子
函数 getControl(ByVal controlName As Integer) As Boolean
Dim txt_Name1 As String = "Hersteller15_" & Str(controlName)
Dim txt_Name2 As String = "Artikel15_" & Str(controlName)
Dim txt_Name3 As String = "Artikel_St_15_" & Str(controlName)
Dim CheckName As String = "checkBox15_08_" & Str(controlName)
如果 CType(Me.panelCheckBox.Controls(CheckName.Replace(" ","")), CheckBox).Enabled = True 那么
CType(Me.panelCheckBox.Controls(CheckName.Replace(" ","")), CheckBox).Enabled = False
MessageBox.Show(txt_Name1)
CType(Me.panelTextbox.Controls(txt_Name1.Replace(" ", "")), TextBox).Text = ""
CType(Me.panelTextbox.Controls(txt_Name2.Replace(" ", "")), TextBox).Text = ""
CType(Me.panelTextbox.Controls(txt_Name3.Replace(" ", "")), TextBox).Text = ""
End If
返回错误
结束函数
'没有命令 Replace(" ", "")) 不起作用 '那必须调整! 'Me.Controls(txt_Name1.Replace(" ", "")), TextBox).Text = "whatever" 'Me.panelCheckBox.Controls(txt_Name1.Replace(" ", "")), TextBox).Text = "whatever" 'Me.GroupBox1.Controls(txt_Name1.Replace(" ", "")), TextBox).Text = "whatever"