Results 1 to 11 of 11

Thread: textfile to streamreader to listview

  1. #1

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196

    textfile to streamreader to listview

    k..i got a listview i need to load a text file into it.....
    text file looks like this...

    Username1
    Password1
    Username2
    Password2

    i got my save to file sub lookin like this...
    Code:
        Private Sub SaveList()
            Dim sTemp As String = Application.StartupPath.ToString & "\idlist.txt"
            Dim sTemp1 As String
            ' Loop through and save all items in list.
            If (LV_IDList.Items.Count > 0) Then
                Try
                    Dim i As Integer
                    ' Create a StreamWriter object to write to the file.
                    Dim writer As New IO.StreamWriter(sTemp, False)
                    ' Write the information to the file one line at a time.
                    i = 0
                    Do
                        If i < LV_IDList.Items.Count Then
                            sTemp1 = LV_IDList.Items(i).Text & vbNewLine & LV_IDList.Items(i).SubItems(1).Text
                            writer.WriteLine(sTemp1)
                            i = i + 1
                        Else
                            Exit Do
                        End If
                    Loop
                    ' Close the StreamWriter object.
                    writer.Close()
                Catch
                End Try
            End If
        End Sub
    now i need to know how to read the file line by line....then i want to add the usernames as an item with the password as the first subitem....

    thx in advance

    AcE
    Last edited by theonetrueace; Apr 25th, 2003 at 12:22 AM.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    First, it helps a lot to use <code> </code> or whatever the board tags are for VB code, it makes what you post a lot more readable.

    as to reading a file line by line:
    Code:
            Dim sr As StreamReader
            Dim strLine, strTemp As String
            Dim strFileName As String = "c:\temp\foo.txt"
    
            ' Open the file to read.
            Try
                sr = File.OpenText(strFileName)
                Try
                    While sr.Peek <> -1
                        strLine = sr.ReadLine
                        'Do some stuff with strLine
                    End While
                Catch
                    MsgBox("Unexpected error reading character file - check the contents of " _
                    + strFileName + ".", MsgBoxStyle.Exclamation)
                End Try
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error opening file!")
            Finally
                'Always close streamreader
                sr.Close()
            End Try

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    PS: example code for adding things programatically to a listview at runtime is here:
    http://www.vbforums.com/showthread.p...hreadid=242066

  4. #4

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    wuz in hurry .. sorry .. tyvm for ur speedy responce .. it's just exactly wut i needed...once again tyvm...

    edit: i just implemented it and it works great....heres wut i did....it lines up great with the save list code i pasted above...

    Code:
        Private Sub LoadList()
            Dim sr As IO.StreamReader
            Dim strLine, strLine2, strTemp As String
    
            Dim strFileName As String = Application.StartupPath.ToString & "\idlist.txt"
    
            ' Open the file to read.
            Try
                sr = IO.File.OpenText(strFileName)
                Try
                    While sr.Peek <> -1
                        strLine = sr.ReadLine
                        strLine2 = sr.ReadLine
                        LV_IDList.Items.Add(strLine).SubItems.Add(strLine2)
                    End While
                Catch
                    MsgBox("Unexpected error reading character file - check the contents of " _
                    + strFileName + ".", MsgBoxStyle.Exclamation)
                End Try
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error opening file!")
            Finally
                'Always close streamreader
                sr.Close()
                MsgBox("ID List Successfully Imported!", MsgBoxStyle.Information, "Done!")
            End Try
    
        End Sub
    Once Again THANKS A Million!

    AcE
    Last edited by theonetrueace; Apr 25th, 2003 at 12:41 AM.

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    LV_IDList.Items.Add(strLine).SubItems.Add(strLine2)

    ^^ I didn't know you could add items to ListView that way, thanks for putting that up.

  6. #6

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    can also add them like this

    Code:
                Dim NewItem As Integer
                NewItem = LV_IDList.Items.Add(TB_ID_Single.Text).Index
                LV_IDList.Items(NewItem).SubItems.Add(TB_Pass_Single.Text)
                LV_IDList.Items(NewItem).SubItems.Add("Ready to Login")
                LV_IDList.Items(NewItem).SubItems.Add("0")
    AcE

  7. #7

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    ive also seen the need for a multi-select & delete function before....heres wut im using in this app....

    Code:
        Private Sub LV_IDList_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_IDList.KeyDown
            Select Case e.KeyData
                Case Keys.Delete
                    Dim SelectedItem As Integer
                    Dim i As Integer
                    SelectedItem = LV_IDList.Items.Count - 1
                    Do
                        If SelectedItem > -1 Then
                            If LV_IDList.Items(SelectedItem).Selected = True Then LV_IDList.Items(SelectedItem).Remove()
                            SelectedItem = SelectedItem - 1
                        Else
                            Exit Do
                        End If
                    Loop
            End Select
        End Sub
    AcE

  8. #8
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: textfile to streamreader to listview

    Sorry for barging in on your thread but iam using the code, thankyou very much.

    I have this code working
    Code:
            Dim strLine, strLine2 As String
    
            Dim strFileName As String = Application.StartupPath & "\Emailed.txt"
    
            ' Open the file to read.
            Try
                sr = IO.File.OpenText(strFileName)
                Try
                    While sr.Peek <> -1
                        strLine = sr.ReadLine
                        strLine2 = sr.ReadLine
                        lsvEmailed.Items.Add(strLine).SubItems.Add(strLine2)
    but if i try to add a second subitem like this

    Code:
            Dim strLine, strLine2, strLine3 As String
    
            Dim strFileName As String = Application.StartupPath & "\Emailed.txt"
    
            ' Open the file to read.
            Try
                sr = IO.File.OpenText(strFileName)
                Try
                    While sr.Peek <> -1
                        strLine = sr.ReadLine
                        strLine2 = sr.ReadLine
                        strLine3 = sr.ReadLine
                        lsvEmailed.Items.Add(strLine).SubItems.Add(strLine2).SubItems.Add(strLine3)
    i get this error

    Error 2 'SubItems' is not a member of 'System.Windows.Forms.ListViewItem.ListViewSubItem'.

    Can i only add 1 subitem this way?

    regards

    toe
    Last edited by toecutter; Oct 17th, 2007 at 09:24 PM.

  9. #9
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: textfile to streamreader to listview

    This is the textfile data format

    ***name and address erased***



    Iam trying to fill a 3 column listview control

    Column 1 = Company Name
    Column 2 = date
    Column 3 = email addess

    Why is "SubItems" not a memeber now while it is when only adding 1 SubItems?

    ps
    this is also attached to this thread
    http://www.vbforums.com/newthread.php
    Last edited by toecutter; Oct 18th, 2007 at 11:14 PM.

  10. #10

    Thread Starter
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196

    Re: textfile to streamreader to listview

    u cant add subitems like that....that would be like adding a subitem to that subitem you just added.... ....you got to do it like this....
    can also add them like this

    Dim NewItem As Integer NewItem = LV_IDList.Items.Add("NEWITEM1").Index
    LV_IDList.Items(NewItem).SubItems.Add("NEWSUBITEM1")
    LV_IDList.Items(NewItem).SubItems.Add("NEWSUBITEM2")
    LV_IDList.Items(NewItem).SubItems.Add("NEWSUBITEM3")


    AcE

  11. #11
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: textfile to streamreader to listview

    yep thanks for that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width