Vb.net 使用名称变量访问多个表单控件
我尝试遍历一组组合框,并使用连接的字符串和变量设置属性,以表示控件的名称。但是,我无法使窗体实例将(String&Integer_变量)识别为其控件之一,因此它无法将任何适当的属性或子例程识别为System.Windows.Forms.Control的成员 我发现了这个方法,它似乎有效(尽管我怀疑),但这感觉像是一个非常笨拙的解决方案。有没有更干净的方法 myTempCount=1到6的Vb.net 使用名称变量访问多个表单控件,vb.net,winforms,vb.net-2010,vb6-migration,Vb.net,Winforms,Vb.net 2010,Vb6 Migration,我尝试遍历一组组合框,并使用连接的字符串和变量设置属性,以表示控件的名称。但是,我无法使窗体实例将(String&Integer_变量)识别为其控件之一,因此它无法将任何适当的属性或子例程识别为System.Windows.Forms.Control的成员 我发现了这个方法,它似乎有效(尽管我怀疑),但这感觉像是一个非常笨拙的解决方案。有没有更干净的方法 myTempCount=1到6的 如果tempValue
如果tempValue
这段代码最初是VBA/VB6,是我通过ArtinSoft的Visual Basic升级助手(VBUC)编写的。FWIW,我正在使用Microsoft Visual Basic 2010 Express。哎哟!!!有一次我搞砸了直播。我记得那是一场噩梦。我的偏好是要么坚持使用服务器端控件,要么将它们作为客户端Javascript/Ajax写入。在你上面的代码中,哪里失败了?有内部异常吗?也许你可以试试这样的方法(C#):
List组合框=新列表
{
ComboxPrinter1,
ComboxPrinter2,
ComboxPrinter3,
ComboxPrinter4,
ComboxPrinter5,
ComboxPrinter6
};
//按位置循环组合框集合
for(int=0;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
将组合框从{_
ComboxPrinter1_
ComboxPrinter2_
ComboxPrinter3_
ComboxPrinter4_
ComboxPrinter5_
ComboxPrinter6_
}
对于i,整数=0到组合框。计数-1
//您可以挂接if逻辑,并使用comboBoxes[i]引用每个组合框
下一个
我希望这有帮助 要回答您的问题:
ComboBox1.SelectedIndex
之所以有效,是因为ComboBox1是表单的ControlCollection中存在的控件Me.ComboBoxPrinter1.SelectedIndex
之所以有效,是因为Me是对表单类的引用,它引用的是控件Me.(“ComboBoxPrinter”和myTempCount)。SelectedIndex
无效,因为字符串ComboxPrinter和myTempCount
是字符串而不是控件Me.Controls.(“ComboBoxPrinter”&myTempCount)。由于相同的原因,SelectedIndex不起作用
For myTempCount = 1 To 6
If Controls.ContainsKey("ComboBox" & myTempCount) Then
CType(Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue
End If
Next
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
我没有在控件前面使用我,因为它引用同一个集合,如果您的控件在另一个集合中,则需要使用该容器。i、 e.如果您使用的是面板
Panel1.Controls.ContainsKey
我再次遇到这个问题,因为多个不同类型的控件需要对公共属性执行相同的操作(如.Text
)。由于不能使用变量来表示CType()
中的控件类型参数,因此必须使用条件和相应的硬编码CType()
命令来获取控件。这就是我想到的:
函数getControl(ByVal controlName作为字符串)作为控件
numCtrls=FrameMain.Controls.Count()
对于I,整数=0到numCtrls-1
如果FrameMain.Controls.Item(I).Name=controlName,则
如果FrameMain.Controls.Item(I)的类型为ComboBox,则
返回CType(FrameMain.Controls(controlName),组合框)
如果结束
如果结束
下一个
端函数
controlName
是连接的字符串名称。因此,您可以使用此函数,其使用方式与前面使用的答案基本相同。CType()
:
大概是这样的:
For myTempCount = 1 To 6
If Controls.ContainsKey("ComboBox" & myTempCount) Then
CType(Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue
End If
Next
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
子按钮2单击(发件人作为对象,e作为事件参数)
'对于i=1到5
如果textBox15_08_St.Text=”“,则
MessageBox.Show(“bite die Anzahl eintragen!”,“Info”,MessageBoxButtons.OK,MessageBoxIcon.Information)
退出子系统如果结束 尺寸i为整数=1 对于panelCheckBox.Controls中的每个c As复选框 尺寸框名称为String=“checkBox15\u 08”&Str(i) Dim CheckName As String=“checkBox15\U 08”&Str(i) BoxName=BoxName.Replace(“,”) 下一个 "完! 端接头 子复选框15_08_1单击(发送者作为对象,e作为事件参数)处理复选框15_08_1。单击 checkBox15_08_1.Checked=Me.getControl(1) 端接头 子checkBox15_08_2单击(发件人作为对象,e作为事件参数)处理checkBox15_08_2。单击 checkBox15_08_2.Checked=Me.getControl(2) 端接头 子checkBox15_08_3单击(发件人作为对象,e作为事件参数)处理checkBox15_08_3。单击 checkBox15_08_3.Checked=Me.getControl(3) 端接头 子复选框15_08_4单击(发送方作为对象,e作为事件参数)处理复选框15_08_4。单击 checkBox15_08_4.Checked=Me.getControl(4) 端接头 子复选框15_08_5Cli
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
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
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 = 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