从Listview将项添加到Listbox中

时间:2009-07-15 02:14:38

标签: vb.net

Public MaintenanceMenuList As ListView

函数AddItems()

 Dim lstModules As New ListBox()

 MaintenanceMenuList.Items.Add("item_1")
 lstModules.Items.Add(MaintenanceMenuList)

结束功能

我收到一个错误,例如“对象引用没有设置为对象的实例”。这里似乎有什么问题?

1 个答案:

答案 0 :(得分:0)

尚未使用/ New关键字

创建MaintenanceMenuList Listview对象
Public MaintenanceMenuList As ListView

Function AddItems()

    Dim lstModules As New ListBox()

    ' if the object is nothing, create it
    If MaintenanceMenuList Is Nothing Then 
        MaintenanceMenuList = New ListView
    End If

    MaintenanceMenuList.Items.Add("item_1")
    lstModules.Items.Add(MaintenanceMenuList)

End Function

此外,您的函数不会返回任何内容或提及它将返回的类型...

编辑 - 添加了对评论的回复

变化:

lstModules.Items.Add(MaintenanceMenuList)

为:

For Each lvi As ListViewItem In MaintenanceMenuList.Items
    lstModules.Items.Add(lvi.Text)
Next