Question Can you add many ListViews to a List(of T) with a loop before ListView is a Control?

dHeather

Active member
Joined
Jun 18, 2012
Messages
27
Programming Experience
Beginner
Hello,

Is it possible to add ListViews to a List(Of T) with a loop if they are not yet Controls on a form? I want to do something similar to the below:

For i AsInteger = 1 To 5
PictureBoxList.Add(
Me.Controls("PictureBox" & i))
Next i

Unlike the above my ListViews are being created during runtime (see below) and are not in my Control List. What I want to know is can I add the code I have highlighted below into a loop? (ImageLists too)

[XCODE]
Public
Class Form1
Dim ListViewList AsNew List(Of ListView)
Dim ListView1, ListView2, ListView3, ListView4, ListView5, ListView6, ListView7, ListView8, ListView9, ListView10 AsNew ListView
Dim ImageListList AsNew List(Of ImageList)
Dim ImageList1, ImageList2, ImageList3, ImageList4, ImageList5, ImageList6, ImageList7, ImageList8, ImageList9, ImageList10 AsNew ImageList
Dim FileGroupTitle() AsString = {"Track Systems", "Hoists & Motors", "Theatre Flying Systems", "SkyDeck", "acouStac Banners", "CAD Blocks Track Systems", "CAD Blocks Hoists & Motors", "CAD Blocks Theatre Flying", "CAD Blocks SkyDeck", "CAD Blocks acouStac Banners"}
Dim CurrentListView AsInteger = 0

Private
Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim MyFile AsString
Dim CSVLine() AsString
Dim CSVCellData() AsString
Dim LastLineText AsString = ""
Dim FileString AsString = ""

' *** CAN THIS AREA BE LOOPED? ***
'********************************
ListViewList.Add(ListView1)
ListViewList.Add(ListView2)
ListViewList.Add(ListView3)
ListViewList.Add(ListView4)
ListViewList.Add(ListView5)
ListViewList.Add(ListView6)
ListViewList.Add(ListView7)
ListViewList.Add(ListView8)
ListViewList.Add(ListView9)
ListViewList.Add(ListView10)
ImageListList.Add(ImageList1)
ImageListList.Add(ImageList2)
ImageListList.Add(ImageList3)
ImageListList.Add(ImageList4)
ImageListList.Add(ImageList5)
ImageListList.Add(ImageList6)
ImageListList.Add(ImageList7)
ImageListList.Add(ImageList8)
ImageListList.Add(ImageList9)
ImageListList.Add(ImageList10)
'********************************

For
i AsInteger = 0 To 9
Me.Controls.Add(ListViewList(i))

With
ListViewList(i)
.View = View.Details
.Width = 300
.Height = 300
.Location =
New Point(10, 10)
.FullRowSelect =
True
.BackColor = Color.White
.BorderStyle = BorderStyle.None
.FullRowSelect =
True
.HeaderStyle = ColumnHeaderStyle.Nonclickable
.SmallImageList = ImageListList(i)
.Columns.Add(FileGroupTitle(i), 248, HorizontalAlignment.Left)
.Visible =
False
EndWith

Dim
LineCount AsInteger = -1
Dim filePath AsString = "E:\Uploads\" & FileGroupTitle(i) & ".csv"
MyFile = My.Computer.FileSystem.ReadAllText(filePath)
CSVLine = Split(MyFile, vbNewLine)

For
Each lineOfText AsStringIn CSVLine
CSVCellData = lineOfText.Split(
",")
ForEach wordOfText AsStringIn CSVCellData

Select
Case wordOfText
Case 1
ListViewList(i).Items(LineCount).Font =
New Font(ListViewList(i).Items(LineCount).Font, FontStyle.Bold Or FontStyle.Underline)
ImageListList(i).Images.Add(
My.Resources.blk)
Case 2
FileString = LastLineText.Substring(LastLineText.Length - 3)
ImageListList(i).Images.Add(
My.Resources.ResourceManager.GetObject(FileString))
Case 3
ListViewList(i).Items(LineCount).ForeColor = Color.LightGray
ImageListList(i).Images.Add(
My.Resources.blk)
CaseElse
LastLineText = wordOfText
LineCount = LineCount + 1
ListViewList(i).Items.Add(wordOfText, LineCount)
EndSelect

Next
wordOfText
Next lineOfText
Next i

ListViewList(CurrentListView).Visible =
True

End
Sub

Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListViewList(CurrentListView).Visible =
False
If CurrentListView = 9 Then
CurrentListView = 0
Else
CurrentListView = CurrentListView + 1
EndIf
ListViewList(CurrentListView).Visible = True
EndSub

Private
Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListViewList(CurrentListView).Visible =
False
If CurrentListView = 0 Then
CurrentListView = 9
Else
CurrentListView = CurrentListView - 1
EndIf
ListViewList(CurrentListView).Visible = True
EndSub
End
Class
[/XCODE]

Thank you very much for your help
 
*** CAN THIS AREA BE LOOPED? ***
Yes (and you don't need those variables)
For i = 1 To 10
    ListViewList.Add(New ListView)
Next

or
For i = 1 To 10
    Dim lv As New ListView
    With lv
        .View = View.Details
        ' etc
    End With
    ListViewList.Add(lv)
Next
 

Latest posts

Back
Top