Я не совсем уверен, что именно вы хотите сделать в 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
РЕДАКТИРОВАТЬ
В ответ на ваш вопрос в комментариях. Я добавил Function
и несколько изменений в ComboTName_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