【问题标题】:How to get items in a ListView and display in a ComboBox?如何在 ListView 中获取项目并在 ComboBox 中显示?
【发布时间】:2018-08-03 14:33:24
【问题描述】:

我创建了两个表单:

  1. Form1 - 此表单包含 ListView1,其中包含员工信息,例如姓名。
  2. Form2 - 此表单包含 ComboBox1

现在我要完成的是从 Form1 的 ListView1 中获取员工姓名,并在 Form2 的 ComboBox1 中显示这些员工姓名。出于练习目的,我通过 Visual Studio 上的 ListViewItem 集合编辑器在 ListView1 上添加了一个项目,以尝试找出如何在 ComboBox1 中显示该虚拟信息。到目前为止,我已经尝试了这些代码行:

ComboBox1.Items.Add(frmListStaffVehicleEntry.ListView1.SelectedItems.Item(1).Text)

ComboBox1.SelectedIndex = frmListStaffVehicleEntry.ListView1.SelectedItems.Item(0).Index

【问题讨论】:

    标签: .net vb.net listview combobox


    【解决方案1】:

    我不确定您是否要将 ListView1 中的所有数据加载到 ComboBox1 或 您想要获取在 ComboBox1 中选择的数据。 你能说清楚吗?以便我可以帮助你......

    【讨论】:

    • 我想将 Listview1 的项目加载到 ComboBox1。据我了解,ListView1 最多只能包含 10-12 个项目,因此在 ComboBox1 中显示所有项目就可以了。
    【解决方案2】:

    如果您想将表单 1 中的列表框中的所有项目复制到表单 2 中的组合框,您可以尝试:(使用您的控件名称)

        For Each jt As String In ListBox3.Items
            Form2.ComboBox1.Items.Add(jt)
        Next
    

    【讨论】:

    • 感谢您的 cmets。我试过代码,但它没有显示我的任何 ListView 项目:` For Each jt As String In frmListEntry.ListView1.Items ComboBox1.Items.Add(jt) Next`
    • 您需要在表单中添加组合框。再次检查我的代码。还要把选项 srikt 放在上面。
    【解决方案3】:

    只需将以下代码添加到您的 Form2_Load 事件

       For i As Integer = 0 To frmListStaffVehicleEntry.ListView1.Items.Count
    
                ComboBox1.Items.Add(frmListStaffVehicleEntry.ListView1.Items.Item(i))
            Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      相关资源
      最近更新 更多