winforms 使用名称变量访问多个表单控件

zhte4eai  于 2022-11-25  发布在  其他
关注(0)|答案(6)|浏览(177)

我试图循环访问一组ComboBox,并使用连接的字符串& variable来设置一个属性,以表示控件的名称。但是,我无法使窗体示例将(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 /VB 6,我把它放在ArtinSoft的Visual Basic升级伴侣(VBUC)中。FWIW,我使用的是Microsoft Visual Basic 2010 Express。

umuewwlo

umuewwlo1#

要回答您的问题:

  1. ComboBox1.SelectedIndex之所以有效,是因为ComboBox1是窗体的ControlCollection中存在的控件
  2. Me.ComboBoxPrinter1.SelectedIndex之所以有效,是因为Me是对Form类的引用,并且它引用了Control。
  3. Me.("ComboBoxPrinter" & myTempCount).SelectedIndex不起作用,因为字符串ComboBoxPrinter & myTempCount是字符串而不是控件。
  4. Me.Controls.("ComboBoxPrinter" & myTempCount).SelectedIndex由于同样的原因不起作用。
    1.其他两个执行严修之所以能运作,是因为您使用字串做为索引键,以查阅和传回您转换为适当型别并设定属性的控件。
    我个人通常使用CType而不是DirectCast。根据这个link,CType和DirectCast之间的主要区别是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,因为它引用了同一个集合,如果您的控件在另一个集合中,您将需要使用该容器。例如,如果您使用的是Panel Panel1.Controls.ContainsKey

chhqkbe1

chhqkbe12#

哎哟!!!我曾经搞砸过Direct Cast。我记得那是一场噩梦。我的偏好是要么坚持使用服务器端控件,要么把它们写成客户端Javascript/ AJAX 。在你上面的代码中,它在哪里失败了?有没有内部异常?

rqcrx0a6

rqcrx0a63#

也许您可以尝试以下代码(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

我希望这对你有帮助!

9avjhtql

9avjhtql4#

我再次遇到了这个问题,有多个不同类型的控件需要对一个公共属性(如.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"
aoyhnmkz

aoyhnmkz5#

大概是这样的:

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
oknrviil

oknrviil6#

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)
        Exit Sub
    End If
    
    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  
Next

'End For
End Sub

Sub checkBox15_08_1Click(sender As Object, e As EventArgs) Handles 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) Handles checkBox15_08_12.Click 
checkBox15_08_12.Checked = Me.getControl(12) 
End Sub

Sub checkBox15_08_13Click(sender As Object, e As EventArgs) Handles 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) Handles checkBox15_08_15.Click 
checkBox15_08_15.Checked = Me.getControl(15) 
End Sub

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

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) Handles checkBox15_08_26.Click
checkBox15_08_26.Checked = Me.getControl(26) 
End Sub
Sub checkBox15_08_27Click(sender As Object, e As EventArgs) Handles checkBox15_08_27.Click 
checkBox15_08_27.Checked = Me.getControl(27) 
End Sub

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

Function 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
Return False
    
End Function

'doesn't work without the command Replace(" ", ""))
'That has to be adjusted!
'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"

相关问题