Home All Groups Group Topic Archive Search About

Creating Multiple Listviews in Code

Author
10 Apr 2006 12:21 PM
Waqas101
Hi,

I have a question regarding the creation of multiple listviews during
run-time. I currently have code that uses a loop to create 5 listviews
with identical properties (excep for the name). Once a listview is
created, it s filled with data loaded from a txt file and then the loop
iterates too creat the next listview.

My problem is that while I am able to create and fill the listviews, I
am not actually able to perform any actions on any of the listviews
except for the one that was created last. By this I mean that if i were
to select an item in a particular listview, only the listview created
last would raise an event (for exmple listview.SelectedIndexChanged).

I know the problem has to do with eveny handling but i'm not sure
exactly what I have to do in order to get each listview to handle
events seperately.

I would appreciate any help on this subject. If it helps I can post my
code...

Thanks,

Waqas

Author
10 Apr 2006 1:19 PM
Peter Proost
Hi you need the addhandler statement, have a look at this example it adds
the click event to all 3 listview

  Private Sub form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim myList As ListView
        For i As Integer = 0 To 2
            myList = New ListView
            myList.Name = "list" & CStr(i)
            myList.Size = New Size(55, 55)
            myList.Location = New Point(60 * (i + 1), 10)
            myList.Items.Add("Hi I'm number" & CStr(i))
            AddHandler myList.Click, AddressOf ListView_Click
            Me.Controls.Add(myList)
        Next
    End Sub

  Private Sub ListView_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
        MsgBox(DirectCast(sender, ListView).Name)
    End Sub

hope this helps,

Greetz Peter


--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

Show quoteHide quote
"Waqas101" <waqas78***@yahoo.co.uk> schreef in bericht
news:1144671689.715200.305050@v46g2000cwv.googlegroups.com...
> Hi,
>
> I have a question regarding the creation of multiple listviews during
> run-time. I currently have code that uses a loop to create 5 listviews
> with identical properties (excep for the name). Once a listview is
> created, it s filled with data loaded from a txt file and then the loop
> iterates too creat the next listview.
>
> My problem is that while I am able to create and fill the listviews, I
> am not actually able to perform any actions on any of the listviews
> except for the one that was created last. By this I mean that if i were
> to select an item in a particular listview, only the listview created
> last would raise an event (for exmple listview.SelectedIndexChanged).
>
> I know the problem has to do with eveny handling but i'm not sure
> exactly what I have to do in order to get each listview to handle
> events seperately.
>
> I would appreciate any help on this subject. If it helps I can post my
> code...
>
> Thanks,
>
> Waqas
>
Author
11 Apr 2006 8:37 AM
Waqas101
Hey thanks a bunch that worked great!

Waqas
Author
11 Apr 2006 9:50 AM
Waqas101
Hey thanks a lot that worked great!

Cheers,

Waqas
Author
11 Apr 2006 1:09 PM
Peter Proost
You're welcome

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

Show quoteHide quote
"Waqas101" <waqas78***@yahoo.co.uk> schreef in bericht
news:1144741616.437836.26810@u72g2000cwu.googlegroups.com...
> Hey thanks a lot that worked great!
>
> Cheers,
>
> Waqas
>