Hi All
i dynamically created gropbox and dynamically insert inside it listview and button

  Dim count1 As Integer
    Dim Listvcounter As Integer
    Dim flashCounter As Integer
    Private Panel As New System.Windows.Forms.GroupBox
    Private ListV As New System.Windows.Forms.ListView
    Private Btn As New System.Windows.Forms.Button
    Private PancLeft As Integer = Me.Controls.OfType(Of GroupBox)().ToList().Count
    Private cLeft As Integer = Me.Panel.Controls.OfType(Of ListView)().ToList().Count
    Private BtncLeft As Integer = Me.Panel.Controls.OfType(Of Button)().ToList().Count


Public Function AddNewListV()
            Panel = New GroupBox
            ListV = New ListView
            Btn = New Button
            Dim name As Integer = 0
            name += 1
            Me.Controls.Add(Panel)
            Panel.Location = New System.Drawing.Point((150 * PancLeft) + 5, 50)
            Panel.Size = New System.Drawing.Size(145, 150)
            Panel.Name = "GroBox" & PancLeft.ToString
            Panel.Text = "Flash Group " & Me.PancLeft.ToString + 1
            PancLeft = PancLeft + 1

            Panel.Controls.Add(ListV)
            ListV.Location = New System.Drawing.Point(5, 20)
            ListV.Size = New System.Drawing.Size(130, 100)
            ListV.Name = "ListV" & cLeft.ToString
            ListV.View = View.List
            cLeft = cLeft + 1

            Panel.Controls.Add(Btn)
            Btn.Location = New System.Drawing.Point(23, 122)
            Btn.Size = New System.Drawing.Size(90, 25)
            Btn.Name = BtncLeft.ToString
            Btn.Text = "Ad Flashs"
            AddHandler Btn.Click, AddressOf Btn_Click
            BtncLeft = BtncLeft + 1
            Return Panel
            Return ListV
            Return Btn
        End Function

then add it to the form by button click

 Private Sub addListViews_Click(sender As Object, e As EventArgs) Handles addListViews.Click


        AddNewListV()

end sub

now when i add two listviews to the form and i want to add files to the first listview the files added into the second listview not the first
how can i fix this plz ?
here is the add files button code :

 Protected Sub Btn_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim ListV As ListView = TryCast(sender, ListView)
        Dim Btn As Button = DirectCast(sender, Button)
        If Main.OpnFlDig.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            Dim filepath() As String = Main.OpnFlDig.FileNames
            For Each Files As String In filepath
                ListV.Items.Add(System.IO.Path.GetFileName(Files))
            Next
            ' MsgBox(ListV.Name)
        End If
    End Sub

best regards

Firstly no need to declare AddNewListV() as a Function. It has nothing to return. Just declare it as Sub Procedure and remove the 3 Return keywords from the end.

Secondly who is the Sender in your Btn_Click Event ?

Dim ListV As ListView = TryCast(sender, ListView)

How do the sender can Cast a ListView object. No need of this line.

You tried here to show a panel dynamicaly. Which have two children, a Button and a ListView object. You can load the file in the listview, which is already stored in the variable ListV. So last one is the targate listview.
To load file to the proper listview the codes should be

Protected Sub Btn_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim pnl As Panel = TryCast(CType(sender, Button).Parent, Panel)

    If Main.OpnFlDig.ShowDialog = System.Windows.Forms.DialogResult.OK Then
        For Each obj As Object In pnl.Controls
            If TypeOf obj Is ListView Then

                Dim lst As ListView = TryCast(obj, ListView)

                Dim filepath() As String = Main.OpnFlDig.FileNames
                For Each Files As String In filepath

                    lst.Items.Add(System.IO.Path.GetFileName(Files))
                Next

                Exit for
            End If
        Next
    End If
End Sub

Hope it can help you.

Edited 1 Year Ago by Shark_1

thank you Shark1 for your replay
i did what you wrote but it gives this error :
An unhandled exception of type 'System.NullReferenceException' occurred in QMNCraft.exe

Additional information: Object reference not set to an instance of an object.
at this part of the code :

For Each obj As Object In pnl.Controls

any suggestions ?!
thank you again

Ok i figuried out whats wrong i declared the panel as GroupBox not a Panel it works now
thanks a lot and best regards

This question has already been answered. Start a new discussion instead.