【问题标题】:Get items and index of dynamic comboBox in VB.NET在VB.NET中获取动态组合框的项目和索引
【发布时间】:2020-03-23 03:28:17
【问题描述】:

我通过函数 combox1Gen() 创建了一个包含八个动态组合框的列表,然后我通过函数 loadComboboxItems() 从值的文本文件中将项目加载到它们。在每个相应的组合框的项目选择上,我需要显示值及其索引。应该在 ComboTName_SelectedIndexChanged() 中放入什么代码? 还有一个问题。我的带有项目的组合框以悬挂的方式加载非常缓慢。我的代码有什么问题? 我的代码如下:

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

【问题讨论】:

    标签: vb.net dynamic combobox


    【解决方案1】:

    我不确定您到底想在 SelectIndexChanged 中做什么,但我展示了如何从被点击的组合中获取值。

    查看 cmets 和行中的解释。

    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
    

    编辑

    在 cmets 中回答您的问题。我添加了一个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
    

    【讨论】:

    • 有什么办法可以得到n的值吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多