Creating Multiple Listviews in Code

W

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
 
P

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top