Problem with adding items and subitems to a listview. PLEASE HELP!

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine

He guys, Im new to this forum and Im working on a vb project that allows users to make there own list of programs, and launch a program they have selected.

But I have the following problem, ones they have added one program and want to add another program by clicking the "add to list" button, the subitem doesnt shows up in the listview.

Heres a picture> http://www.mediafire.com/view/?bld94er7htv1k94

Im using the follwing code:
Public Class Form1
Private myCoolFile As String = "C:Startup ManangerList.txt" // your file.


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.InitialDirectory = "C:"
OpenFileDialog1.FileName = "Open A File..."
OpenFileDialog1.ShowDialog()

TextBox1.Text = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


If TextBox1.Text = "" Then
MsgBox("Please Choose a File")
End If


ListView1.Items.Add(TextBox3.Text)

ListView1.Items(0).SubItems.Add(TextBox1.Text)

TextBox1.Clear()
TextBox3.Clear()


End Sub


Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

IO.Directory.CreateDirectory("C:Startup Mananger")
Dim myWriter As New IO.StreamWriter(myCoolFile)
For Each myItem As ListViewItem In ListView1.Items
myWriter.WriteLine(myItem.Text & "#" & myItem.SubItems(1).Text) // write Item and SubItem.
Next
myWriter.Close()

End Sub


Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If IO.File.Exists(myCoolFile) Then // check if file exists.

Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolFile) // load your file as a string array.

For Each line As String In myCoolFileLines // loop thru array list.

Dim lineArray() As String = line.Split("#") // separate by "#" character.

Dim newItem As New ListViewItem(lineArray(0)) // add text Item.

newItem.SubItems.Add(lineArray(1)) // add SubItem.
ListView1.Items.Add(newItem) // add Item to ListView.
Next
End If
End Sub

View the full article
 
Back
Top