English 中文(简体)
使用名称的变量访问多窗窗窗控件
原标题:accessing multiple form controls using a variable for the name
最佳回答

回答你的问题:

  1. ComboBox1.SelectedIndex works because ComboBox1 is the control that is present in the Form s ControlCollection
  2. Me.ComboBoxPrinter1.SelectedIndex works because Me is a reference to your Form class an it is referencing the Control.
  3. Me.("ComboBoxPrinter" & myTempCount).SelectedIndex doesn t work because the string ComboBoxPrinter & myTempCount is a string not a Control.
  4. Me.Controls.("ComboBoxPrinter" & myTempCount).SelectedIndex doesn t work for the same reasons.
  5. The other two instances work because you are using the string as a key to lookup and return the control which you the cast to the proper type and set your property.

我个人通常使用直接电荷以外的CType。根据此 < a href=> http://www.novick software.com/TipSandTricks/tip-visibal- basic- cty- ctype-directcast-dot-net.htm" rel=“nofollow”>>link ,CType和直接电荷之间的主要区别是,直接电荷必须是在缩小或扩大转换中使用CType的确切类型。直接电荷更精密,但更精密。

他说你可以做这样的事情:

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

我不在控件前使用我, 因为控件指的是同一收藏, 如果您的控件在另一个收藏中, 您需要使用该容器, 也就是说, 如果您使用面板 < code> 板1. controls. containsKey 。

问题回答

哎呀!!我曾与直飞公司乱搞过一次,我记得那是一场恶梦,我宁愿坚持服务器侧控件,或将它们写成客户端。Javascript/Ajax。上面的代码中哪里有问题?有什么内部例外?

或许你可以尝试这样(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

我希望这能帮上忙!

我再次遇到这个问题,因为有不同种类的多重控制,需要在共同属性(如 .Text )上执行相同的操作(如 .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 is the concated string name. 所以, 您可以使用此函数与之前使用的 CType () 解答方法大致相同 :

getControl("TextBox" & myTempCount).Text = "whatever"

像这样的事情:

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
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"




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签