2

我正在尝试遍历一组组合框并使用连接的字符串和变量设置一个属性来表示控件的名称。但是,我无法让表单实例将 (String & Integer_Variable) 识别为其控件之一——因此它无法将任何适当的属性或子例程识别为 System.Windows.Forms 的成员。控制。

在 SO 上找到了 DirectCast 解决方案,它似乎可以工作(虽然我很怀疑),但这感觉像是一个非常笨拙的解决方案。有没有更清洁的方法来做到这一点?

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。

4

6 回答 6

3

要回答您的问题:

  1. ComboBox1.SelectedIndex有效,因为 ComboBox1 是表单的 ControlCollection 中存在的控件
  2. Me.ComboBoxPrinter1.SelectedIndex之所以有效,是因为 Me 是对您的 Form 类的引用,并且它正在引用控件。
  3. Me.("ComboBoxPrinter" & myTempCount).SelectedIndex不起作用,因为字符串ComboBoxPrinter & myTempCount是字符串而不是控件。
  4. Me.Controls.("ComboBoxPrinter" & myTempCount).SelectedIndex由于相同的原因不起作用。
  5. 其他两个实例之所以有效,是因为您使用字符串作为键来查找并返回您转换为正确类型的控件并设置您的属性。

我个人通常使用 DirectCast 以外的 CType。根据此链接,CType 和 DirectCast 之间的主要区别在于 DirectCast 必须是可用于缩小或扩大转换的确切类型。DirectCast 更有效,尽管更挑剔。

话虽这么说,你可以做这样的事情:

For myTempCount = 1 To 6
    If Controls.ContainsKey("ComboBox" & myTempCount) Then
        CType(Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue
    End If
Next

我没有在 Controls 前面使用 Me,因为它指的是同一个集合,如果您的控件在另一个集合中,您将需要使用该 Container。即,如果您使用的是面板Panel1.Controls.ContainsKey

于 2012-05-23T01:46:08.933 回答
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"
于 2012-07-05T04:23:42.940 回答
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 的上述代码:

将组合框作为新列表(控件)()从 { _
    组合框打印机 1, _
    ComboBoxPrinter2, _
    ComboBoxPrinter3, _
    ComboBoxPrinter4, _
    ComboBoxPrinter5, _
    ComboBoxPrinter6 _
}

For i As Integer = 0 To comboBoxes.Count - 1
  // 你可以挂钩你的 if 逻辑并使用 comboBoxes[i] 引用每个组合框
下一个

我希望这有帮助!

于 2012-05-23T01:22:08.853 回答
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
于 2014-10-26T09:12:23.030 回答
0

哎哟!!!我曾经和 Direct Cast 搞过一次。我记得那是一场噩梦。我的偏好是要么坚持使用服务器端控件,要么将它们作为客户端 Javascript/Ajax 编写。你上面的代码在哪里失败了?任何内部异常?

于 2012-05-23T00:22:20.863 回答
0

Sub Button2Click(sender As Object, e As EventArgs) 'For i = 1 To 5 If textBox15_08_St.Text = "" Then 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

将 i 调暗为整数 = 1

对于每个 c 作为 CheckBox 在 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  

下一个

'End For End Sub

Sub checkBox15_08_1Click(sender As Object, e As EventArgs) 处理 checkBox15_08_1.Click checkBox15_08_1.Checked = Me.getControl(1) End Sub

Sub checkBox15_08_2Click(sender As Object, e As EventArgs) Handles checkBox15_08_2.Click checkBox15_08_2.Checked = Me.getControl(2) End Sub

Sub checkBox15_08_3Click(sender As Object, e As EventArgs) Handles checkBox15_08_3.Click checkBox15_08_3.Checked = Me.getControl(3) End Sub

Sub checkBox15_08_4Click(sender As Object, e As EventArgs) Handles checkBox15_08_4.Click checkBox15_08_4.Checked = Me.getControl(4) End Sub

Sub checkBox15_08_5Click(sender As Object, e As EventArgs) Handles checkBox15_08_5.Click checkBox15_08_5.Checked = Me.getControl(5) End Sub

Sub checkBox15_08_6Click(sender As Object, e As EventArgs) Handles checkBox15_08_6.Click checkBox15_08_6.Checked = Me.getControl(6) End Sub

Sub checkBox15_08_7Click(sender As Object, e As EventArgs) Handles checkBox15_08_7.Click checkBox15_08_7.Checked = Me.getControl(7) End Sub

Sub checkBox15_08_8Click(sender As Object, e As EventArgs) Handles checkBox15_08_8.Click checkBox15_08_8.Checked = Me.getControl(8) End Sub

Sub checkBox15_08_9Click(sender As Object, e As EventArgs) Handles checkBox15_08_9.Click checkBox15_08_9.Checked = Me.getControl(9) End Sub

Sub checkBox15_08_10Click(sender As Object, e As EventArgs) Handles checkBox15_08_10.Click checkBox15_08_10.Checked = Me.getControl(10) End Sub

Sub checkBox15_08_11Click(sender As Object, e As EventArgs) Handles checkBox15_08_11.Click checkBox15_08_11.Checked = Me.getControl(11) End Sub

Sub checkBox15_08_12Click(sender As Object, e As EventArgs) 处理 checkBox15_08_12.Click checkBox15_08_12.Checked = Me.getControl(12) End Sub

Sub checkBox15_08_13Click(sender As Object, e As EventArgs) 处理 checkBox15_08_13.Click checkBox15_08_13.Checked = Me.getControl(13) End Sub

Sub checkBox15_08_14Click(sender As Object, e As EventArgs) Handles checkBox15_08_14.Click checkBox15_08_14.Checked = Me.getControl(14) End Sub

Sub checkBox15_08_15Click(sender As Object, e As EventArgs) 处理 checkBox15_08_15.Click checkBox15_08_15.Checked = Me.getControl(15) End Sub

Sub checkBox15_08_16Click(sender As Object, e As EventArgs) 处理 checkBox15_08_16.Click checkBox15_08_16.Checked = Me.getControl(16) End Sub

Sub checkBox15_08_17Click(sender As Object, e As EventArgs) Handles checkBox15_08_17.Click checkBox15_08_17.Checked = Me.getControl(17) End Sub

Sub checkBox15_08_18Click(sender As Object, e As EventArgs) Handles checkBox15_08_18.Click checkBox15_08_18.Checked = Me.getControl(18) End Sub

Sub checkBox15_08_19Click(sender As Object, e As EventArgs) Handles checkBox15_08_19.Click checkBox15_08_19.Checked = Me.getControl(19) End Sub

Sub checkBox15_08_20Click(sender As Object, e As EventArgs) Handles checkBox15_08_20.Click checkBox15_08_20.Checked = Me.getControl(20) End Sub

Sub checkBox15_08_21Click(sender As Object, e As EventArgs) Handles checkBox15_08_21.Click checkBox15_08_21.Checked = Me.getControl(21) End Sub

Sub checkBox15_08_22Click(sender As Object, e As EventArgs) Handles checkBox15_08_22.Click checkBox15_08_22.Checked = Me.getControl(22) End Sub

Sub checkBox15_08_23Click(sender As Object, e As EventArgs) 处理 checkBox15_08_23.Click checkBox15_08_23.Checked = Me.getControl(23) End Sub Sub 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) Handles checkBox15_08_25.Click checkBox15_08_25.Checked = Me.getControl(24) End Sub

Sub checkBox15_08_26Click(sender As Object, e As EventArgs) 处理 checkBox15_08_26.Click checkBox15_08_26.Checked = Me.getControl(26) End Sub 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) Handles checkBox15_08_28.Click checkBox15_08_28.Checked = Me.getControl(28) End Sub

Sub checkBox15_08_29Click(sender As Object, e As EventArgs) Handles checkBox15_08_29.Click checkBox15_08_29.Checked = Me.getControl(29) End Sub

Sub checkBox15_08_30Click(sender As Object, e As EventArgs) Handles checkBox15_08_30.Click checkBox15_08_30.Checked = Me.getControl(30) End Sub

函数 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)

If CType(Me.panelCheckBox.Controls(CheckName.Replace(" ","")), CheckBox).Enabled = True Then

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 = "不管什么”'Me.GroupBox1.Controls(txt_Name1.Replace(" ", "")), TextBox).Text = "whatever"

于 2021-06-13T17:50:50.923 回答