简体   繁体   English

从另一种形式更新ListView

[英]update ListView from another form

I tried all methods by users here but none seems to work for me. 我在这里尝试了用户的所有方法,但似乎没有一种对我有用。 I want to update a ListView in Form1 from Form2 in vb.net but nothing happens when I launch this method. 我想从vb.net中的Form2更新Form1中的ListView,但是启动此方法时没有任何反应。

Public Sub checkFoundList()
    For Each item In myListView.Items
        If Not File.Exists(item.SubItems(2).Text) Then
            myListView.Items.Remove(item)
        End If
    Next
End Sub

This method is on Form1 and when I launch it here it works fine. 此方法在Form1上,当我在这里启动它时,它可以正常工作。 But if I call it from Form2, it doesn't. 但是,如果我从Form2调用它,则不会。

In Form2 I just call it with: 在Form2中,我只是用以下方式调用它:

Form1.checkFoundList()

I tried also to put the modifier Public to myListView but still doesn't work. 我也尝试将修饰符Public myListView但仍然无法正常工作。 Also the methods explained by some users like using events doesn't work. 同样,某些用户解释的方法(例如使用事件)无效。 Really weird. 真的很奇怪。

Is ListView a special control? ListView是一个特殊的控件吗?

One problem you will have is you are modifying the items in the list as you are enumerating it with a For Each statement. 您将遇到的一个问题是,在使用For Each语句枚举时,您正在修改列表中的项目。 This will cause problems when you delete an item. 删除项目时,这将导致问题。

Instead enumerate it with a For statement working backwards so the indexes don't shift when you are removing an item: 而是使用向后工作的For语句枚举它,以便在删除项目时索引不会移动:

Public Sub checkFoundList()
    For i = myListView.Items.Count - 1 To 0 Step -1
        Dim item As <TypeTheListViewHolds> = myListView.Items(i)
        If Not File.Exists(item.SubItems(2).Text) Then
            myListView.Items.RemoveAt(i)
        End If
    Next
End Sub

I have just adapted the code you provided (without knowing what myListView holds), but the methodology would be the same regardless of the datatype. 我刚刚修改了您提供的代码(不知道myListView拥有什么),但是无论数据类型如何,方法都是相同的。

With regards to calling it from Form2 , make sure you are calling checkFoundList from an instance of Form1 . 关于从Form2调用它,请确保您从Form1实例调用checkFoundList Something like: 就像是:

' Class variable in Form2 which has an instance of Form1.
Private _form1 As Form1

' New Form2 method.
' Pass an instance of Form1 to the constructor of Form2.
' This way this instance of Form2 will "know" about a Form1 object.
Public Sub New(form1Object As Form1)
   ' Initialization code.

   ' Set the reference to Form1 in Form2
   _form1 = form1Object
End Sub

Public Sub Form2Method()
    _form1.checkFoundList
End Sub

As mentioned, it would be helpful to see the error you are getting. 如前所述,查看您遇到的错误将很有帮助。 But it is most likely because Form2 doesn't have a reference to Form1. 但这很可能是因为Form2没有对Form1的引用。 One of many ways to fix this is to set Form2's owner to Form1. 解决此问题的多种方法之一是将Form2的所有者设置为Form1。

In Form1 when you create Form2 set its owner: 在Form1中创建Form2时,设置其所有者:

Dim f2 As Form2 = New Form2()
f2.Owner = Me
f2.ShowDialog()

In Form2 get a reference to Form1 and access the list box, in this case I access a simple text box: 在Form2中,获取对Form1的引用并访问列表框,在这种情况下,我将访问一个简单的文本框:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim f1 As Form1 = DirectCast(Me.Owner, Form1)
    f1.TextBox1.Text = "Hello World"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM