none
Passing List View as parameter RRS feed

  • Question

  • HI All,

    I have a Class that create a list view with just two column and 5 rows

    Then I pass the ListView to another class that is a user control that as an empty ListView created by designer (UCFilterStatusListView)

    In the User Control I get the passed ListView in this way

    Public Sub ShowFilterStatus(ByRef pListView As ListView)
    		UCFilterStatusListView.Clear()
    		UCFilterStatusListView = pListView
    		UCFilterStatusListView.View = View.Details
    		UCFilterStatusListView.Show()
    End sub

    The problem is that the listview is not shown in this way

    If I use this code with loop to build the ListView in the UserControl i get the ListView shown

    Public Sub ShowFilterStatus(ByRef pListView As ListView)
    
    		Dim S(1) As String
    		For i = 0 To pListView.Items.Count - 1
    			S(0) = pListView.Items(i).SubItems(0).Text
    			S(1) = pListView.Items(i).SubItems(1).Text
    			Dim ListItem = New ListViewItem(S)
    			UCFilterStatusListView.Items.Add(ListItem)
    		Next
    		UCFilterStatusListView.Show()
    
    	End Sub

    How can I show the ListView without the loop ?


    Saturday, May 5, 2018 6:11 AM

Answers

  • Try this:

    Public Sub ShowFilterStatus(pListView As ListView)
       Dim p = UCFilterStatusListView.Parent
       p.Controls.Add(pListView)
       pListView.Bounds = UCFilterStatusListView.Bounds
       pListView.Visible = True
       p.Controls.Remove(UCFilterStatusListView)
    UCFilterStatusListView = p End Sub


    • Edited by Viorel_MVP Saturday, May 5, 2018 7:34 AM
    • Marked as answer by Claudio111 Saturday, May 5, 2018 8:17 AM
    Saturday, May 5, 2018 7:33 AM

All replies

  • A reference type as a listview is always passed by its reference. By reference means that there are 2 references passed.

    Passing a listview by reference has no sense, the same as using the data from a control. Use the original data

    However, your problem is here that you use a sub instead of a function. 


    Success
    Cor

    Saturday, May 5, 2018 7:21 AM
  • Try this:

    Public Sub ShowFilterStatus(pListView As ListView)
       Dim p = UCFilterStatusListView.Parent
       p.Controls.Add(pListView)
       pListView.Bounds = UCFilterStatusListView.Bounds
       pListView.Visible = True
       p.Controls.Remove(UCFilterStatusListView)
    UCFilterStatusListView = p End Sub


    • Edited by Viorel_MVP Saturday, May 5, 2018 7:34 AM
    • Marked as answer by Claudio111 Saturday, May 5, 2018 8:17 AM
    Saturday, May 5, 2018 7:33 AM
  • Thanks Viorel it works

    Saturday, May 5, 2018 8:17 AM
  • Just a correction:

        UCFilterStatusListView = pListView

    Saturday, May 5, 2018 10:31 AM