Multiple List Views ?

  • Thread starter Thread starter Jm
  • Start date Start date
J

Jm

Hi All

Is there a way to have multiple list views created during program runtime in
code, which can have objects added, removed and later on the listview itself
be removed, whilst keeping the previous listview(s) hidden or disabled under
the original ?

Thanks
 
Jm said:
Is there a way to have multiple list views created during program runtime
in
code, which can have objects added, removed and later on the listview
itself
be removed, whilst keeping the previous listview(s) hidden or disabled
under
the original ?

Yes, you can do that:

\\\
Dim i As Integer
For i = 1 To 10
Dim lvw As New ListView()
With lvw
.Location = ...
...
End With
Me.Controls.Add(lvw)
Next i
///

To add a handler to the listview controls' events, you can use
'AddHandler'/'RemoveHandler'. Inside the event handlers you can check the
'sender' parameter to get a reference to the listview control that raised
the event:

\\\
Dim SourceControl As ListView = DirectCast(sender, ListView)
....
///
 
Hi Cor

Thanks for quick reply. I checked out the site you said and ive got most of
the idea of it i think. But im doing something wrong with the coding of the
events. This is the code im using, excluding all code around it for ease of
copy and paste


Public SubLevelCount As Integer
Public SubLstView() As ListView
Public SubImgLst() As ImageList
Public Console As New frmMain
***

ReDim Preserve SubLstView(SubLevelCount)
ReDim Preserve SubImgLst(SubLevelCount)
SubLstView(SubLevelCount) = New ListView
SubImgLst(SubLevelCount) = New ImageList
SubImgLst(SubLevelCount).ImageSize = New Size(32, 32)
SubImgLst(SubLevelCount).Images.Add(Console.imgLstMain.Images(1))
SubLstView(SubLevelCount).Left = 8
SubLstView(SubLevelCount).Top = 32
SubLstView(SubLevelCount).Width = 560
SubLstView(SubLevelCount).Height = 224
SubLstView(SubLevelCount).BackColor = Drawing.Color.FromArgb(58, 110, 165)
SubLstView(SubLevelCount).ForeColor = System.Drawing.Color.White
SubLstView(SubLevelCount).MultiSelect = False
SubLstView(SubLevelCount).Parent = Console
SubLstView(SubLevelCount).Sorting = SortOrder.Ascending
SubLstView(SubLevelCount).SmallImageList = SubImgLst(SubLevelCount)
SubLstView(SubLevelCount).LargeImageList = SubImgLst(SubLevelCount)

Now heres where i wasnt sure how to go

SubLstView(SubLevelCount).Controls.Add(SubLstView(SubLevelCount))
AddHandler SubLstView(SubLevelCount).ItemActivate, AddressOf
SubLstView_ItemActivate


The addhandler line confused me. I wanted to add a handler for the item
activate event. So when a user double clicks i can run the necessary code. I
tried to adapt it to my code but at the end for the AddressOf part it seems
to fail and im not sure what to do from here ? Any Ideas ?

Thanks
 
Little bit of extra info. I figured out the problem was me not reading the
google message further so i added the sub to handle the event with blank
code for the time being. But when the code gets to the point of adding the
control it gives me this message:

A circular control reference has been made. A control cannot be owned or
parented to itself

this is the extra code i added since last post

Public Sub Sublstview_ItemActivate(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & thisbutton.Text)
End Sub
 
Hi Cor

Sorry about the quick posts just figured out the problem, because im using
the code inside a module i cant use Me for

SubLstView(SubLevelCount).Controls.Add(SubLstView(SubLevelCount))

so instead i used the form that i was referencing in the code

Console.Controls.Add(SubLstView(SubLevelCount))

Thanks for the link, wouldnt have figured it out without it. So many thanks
once again
 
JM,

Can you have a look from the sample I made from your code

You need for this to set a button in top of the form, I made as well your
listview a little bit smaller and used because of that the mouse down.

\\\\
Public SubLevelCount As Integer
Public SubImgLst() As ImageList
Friend myListViews As New ArrayList
Friend myListViewCount As Integer
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim SubLstView As New ListView
' SubImgLst = New ImageList
' SubImgLst.ImageSize = New Size(32, 32)
'SubImgLst.Images.Add(Console.imgLstMain.Images(1))
SubLstView.Left = 8 + myListViewCount * 60
SubLstView.Top = 32
SubLstView.Width = 56
SubLstView.Height = 24
SubLstView.Name = (myListViewCount + 1).ToString
SubLstView.BackColor = Drawing.Color.FromArgb(58, 110, 165)
SubLstView.ForeColor = System.Drawing.Color.White
SubLstView.MultiSelect = False
' SubLstView.Parent = Console
SubLstView.Sorting = SortOrder.Ascending
' SubLstView.SmallImageList = SubImgLst
' SubLstView.LargeImageList = SubImgLst
myListViews.Add(SubLstView)
AddHandler SubLstView.MouseDown, AddressOf ListView_MouseDown
Me.Controls.Add(SubLstView)
myListViews.Add(SubLstView)
myListViewCount += 1
End Sub
Private Sub ListView_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
If myListViews.Count > 0 Then
Dim mylastlistview As ListView = _
DirectCast(myListViews(myListViews.Count - 1), ListView)
myListViews.RemoveAt(myListViews.Count - 1)
Me.Controls.Remove(mylastlistview)
myListViewCount -= 1
End If
End Sub
///

What is does is creating a listview when the button is clicked and removes
the last when there is a mouse down on whatever listview.

Is it alreayd 2005 there in NZ?
Than Happy newsyear

I hope this helps?

Cor
 
Hi Cor

Thankyou for the new code, havent seen a prob with it yet. And lol yes is it
NY here, only 30 mins ago, and not in Nz, in Aus(Sydney). For some reason my
isp's news server's all post out through NZ although it has one for each
state in aus for reading the mail. Happy new years to you :-)
 

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

Back
Top