在VB.NET中获取动态组合框的项目和索引
我通过函数combox1Gen()创建了一个包含八个动态组合框的列表,然后通过函数loadComboboxItems()从值的文本文件中加载了项。在每个组合框的项目选择中,我需要显示值及其索引。在ComboTName\u SelectedIndexChanged()中应该放什么代码? 还有一个问题。我的组合框中的项目以挂起的方式加载得非常慢。我的代码怎么了? 我的代码如下:在VB.NET中获取动态组合框的项目和索引,vb.net,dynamic,combobox,Vb.net,Dynamic,Combobox,我通过函数combox1Gen()创建了一个包含八个动态组合框的列表,然后通过函数loadComboboxItems()从值的文本文件中加载了项。在每个组合框的项目选择中,我需要显示值及其索引。在ComboTName\u SelectedIndexChanged()中应该放什么代码? 还有一个问题。我的组合框中的项目以挂起的方式加载得非常慢。我的代码怎么了? 我的代码如下: Public ComboBoxesTname As New List(Of ComboBox) Dim
Public ComboBoxesTname As New List(Of ComboBox)
Dim i As Integer = 0
Do While i <= 7
Combo1Gen(i)
i = i + 1
Loop
loadComboboxItems()
Private Function Combo1Gen(ByVal n As Integer) As Boolean
Try
Dim newCombo As New ComboBox
With newCombo
.Name = "MyComboBox1" & n.ToString
.Left = 110
.Top = 120 + (52 * n) + 20
.Width = 180
.Height = 20
.Visible = True
End With
ComboBoxesTname.Add(newCombo)
GroupBox1.Controls.Add(newCombo)
GroupBox1.AutoSize = True
AddHandler newCombo.SelectedIndexChanged, AddressOf ComboTName_SelectedIndexChanged
Return True
Catch ex As Exception
MessageBox.Show(ex.ToString)
Return False
End Try
End Function
Private Function loadComboboxItems() As Boolean
Try
Dim listT As New List(Of String)()
listT = ReadVars.readVars(GetFolderPath.GetFolderPath("\vars\"), "items.txt")
For i = 0 To ComboBoxesTname.Count - 1
ComboBoxesTname(i).Items.AddRange(listT.ToArray)
Next
Return True
Catch ex As Exception
Debug.WriteLine(ex.ToString)
MessageBox.Show("Error: " & Err.ToString)
Return False
End Try
Return False
End Function
Private Sub ComboTName_SelectedIndexChanged()
End Sub
Public ComboBoxesTname作为(ComboBox的)新列表
尺寸i为整数=0
虽然我不确定您想在SelectIndexChanged
中做什么,但我演示了如何从单击的组合中获取值
请参见第行中的注释和解释
Public ComboBoxesTname As New List(Of ComboBox)
Private Sub LoadCombos()
For i = 0 To 7
Combo1Gen(i)
Next
loadComboboxItems()
End Sub
'Since you never use the return value I changed this to a Sub
Private Sub Combo1Gen(ByVal n As Integer)
Dim newCombo As New ComboBox
With newCombo
.Name = "MyComboBox1" & n.ToString
.Left = 110
.Top = 10 + (52 * n) + 20
.Width = 180
.Height = 20
.Visible = True
End With
ComboBoxesTname.Add(newCombo)
GroupBox1.Controls.Add(newCombo)
GroupBox1.AutoSize = True
AddHandler newCombo.SelectedIndexChanged, AddressOf ComboTName_SelectedIndexChanged
End Sub
'Since I don't have access to ReadVars I created an arbitrary array to test
Private listT As String() = {"Mathew", "Mark", "Luke", "John"}
'You should have readVars return an array of strings
'Create this once as a Form level variable so you don't have to read the file over and over
'Private listT As String() = ReadVars.readVars(GetFolderPath.GetFolderPath("\vars\"), "items.txt")
'Since you never use the return value I changed this to a Sub
Private Sub loadComboboxItems()
For i = 0 To ComboBoxesTname.Count - 1
ComboBoxesTname(i).Items.AddRange(listT)
Next
End Sub
'Added the appropriate parameters
Private Sub ComboTName_SelectedIndexChanged(sender As Object, e As EventArgs)
'Cast sender to a ComboBox so we can use the Properties of a ComboBox
Dim Combo = DirectCast(sender, ComboBox)
Dim Name = Combo.Name
Dim Item = Combo.SelectedItem
MessageBox.Show($"The selected item in {Name} is {Item}")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadCombos()
End Sub
编辑
在评论中回答您的问题。我添加了一个函数
,并对组合名\u SelectedIndexChanged
Private Sub ComboTName_SelectedIndexChanged(sender As Object, e As EventArgs)
'Cast sender to a ComboBox so we can use the Properties of a ComboBox
Dim Combo = DirectCast(sender, ComboBox)
Dim Name = Combo.Name
Dim N As Integer = GetValueOfN(Name)
Dim Item = Combo.SelectedItem
MessageBox.Show($"The selected item in {Name} is {Item} The number of the combo cox is {N}")
End Sub
Private Function GetValueOfN(ComboName As String) As Integer
Dim NumString = ComboName.Substring(ComboName.IndexOf("1") + 1)
Return CInt(NumString)
End Function
有没有办法得到n的值?