Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 117,604 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 2,374 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Saving Listview contents with heading to .txt

 
Reply to this topicStart new topic

Saving Listview contents with heading to .txt

gram999
post 30 Jun, 2008 - 02:53 PM
Post #1


D.I.C Head

**
Joined: 21 Jan, 2008
Posts: 81



Thanked 1 times
My Contributions


As the heading mentions I am trying to save the contents of a listview with headings to a .txt file. I have been able to save the data to a .txt file without problems. The issue arises when I am trying to get headings. This is the code I have been trying.

CODE


Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click

        Dim outputstream As StreamWriter = File.CreateText("c:\listview.txt")

        Dim s As Short
        Dim newstr(1) As String
        
        Dim I As Integer

        Dim TextA As String
        Dim TextB As String
        Dim TextC As String
        Dim TextD As String
        Dim TextE As String

        Dim ItemCount As Integer

        ItemCount = Me.ListViewSummary.Items.Count

        For I = 0 To ItemCount - 1
            For s = 0 To 1
                newstr(s) = ListViewSummary.Items.Item(I).SubItems(s).Text
            Next

            TextA = newstr(0)
            TextB = newstr(1)
            TextC = newstr(2)
            TextD = newstr(3)
            TextE = newstr(4)

            outputstream.WriteLine(TextA & "|" & TextB & "|" & TextC & "|" & TextD & "|" & TextE)
        Next I

        outputstream.Close()
    End Sub



The code is hanging on TextC = newstr(2)

The error is "Make sure that the maximum index on a list is less than the list size." I know that this is referring to the array but I have no idea how to cure. All ideas are welcome. Thanks!

This post has been edited by gram999: 30 Jun, 2008 - 02:54 PM
User is offlineProfile CardPM

Go to the top of the page


jayman9
post 30 Jun, 2008 - 04:15 PM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,445



Thanked 27 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


You have declared the array to only hold two elements. You need to increase its size if you want to use index number higher than 1.
This:
CODE
Dim newstr(1) As String


Should be:
CODE

Dim newstr(5) As String


Need to change loop to:
CODE

            For s = 0 To 4
                newstr(s) = ListViewSummary.Items.Item(I).SubItems(s).Text
            Next
User is offlineProfile CardPM

Go to the top of the page

gram999
post 30 Jun, 2008 - 05:32 PM
Post #3


D.I.C Head

**
Joined: 21 Jan, 2008
Posts: 81



Thanked 1 times
My Contributions


Thanks Jayman. I had tried the s = 0 To 4 and it was still giving me the error. The part I was missing was the Dim nwstr (5).
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 30 Jun, 2008 - 09:07 PM
Post #4


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,445



Thanked 27 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


You are very welcome. Glad to have helped.
User is offlineProfile CardPM

Go to the top of the page

narmer93
post 1 Jul, 2008 - 03:49 PM
Post #5


D.I.C Head

**
Joined: 13 Mar, 2008
Posts: 220



Thanked 1 times
My Contributions


there is something not correct in that code
vb
Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click

Dim outputstream As StreamWriter = File.CreateText("c:\listview.txt")

Dim s As Short
Dim newstr(1) As String

Dim I As Integer

Dim TextA As String
Dim TextB As String
Dim TextC As String
Dim TextD As String
Dim TextE As String

Dim ItemCount As Integer

ItemCount = Me.ListViewSummary.Items.Count

For I = 0 To ItemCount - 1
For s = 0 To 1
newstr(s) = ListViewSummary.Items.Item(I).SubItems(s).Text
Next

TextA = newstr(0)
TextB = newstr(1)
TextC = newstr(2)
TextD = newstr(3)
TextE = newstr(4)

outputstream.WriteLine(TextA & "|" & TextB & "|" & TextC & "|" & TextD & "|" & TextE)
Next I

outputstream.Close()
End Sub

the error is in this part i think
vb
 Dim outputstream As StreamWriter 

there is nothing called streamwriter
if so,can anybody tell me how can i create that textfile or anytype of files,or ....... is it possible to delete files too and create folders and microsoft word documents and save changes into them? biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 1 Jul, 2008 - 04:56 PM
Post #6


DICula

Group Icon
Joined: 29 May, 2008
Posts: 603



Thanked 38 times

Dream Kudos: 2000
My Contributions


Try the following
vb
Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
Dim outputstream As io.StreamWriter = System.IO.File.CreateText("c:\listview.txt")
End Sub

or
vb

dim outputstream as new io.streamwriter("c:listview.txt")
User is offlineProfile CardPM

Go to the top of the page

narmer93
post 2 Jul, 2008 - 02:30 AM
Post #7


D.I.C Head

**
Joined: 13 Mar, 2008
Posts: 220



Thanked 1 times
My Contributions


oh yea the last one is ok it worked,but it can't work with delete ,copy or move if i use
vb
 Dim outputstream As io.StreamWriter =

it is only
vb
system.io.file.Delete'or .copy,or.move.....'("'the file to delete or move'")

oh and now how can i make a text that is saved to a notepad that i create?
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 2 Jul, 2008 - 03:35 AM
Post #8


DICula

Group Icon
Joined: 29 May, 2008
Posts: 603



Thanked 38 times

Dream Kudos: 2000
My Contributions


QUOTE
can't work with delete ,copy or move if i use

File operations (delete, copy, move etc) are performed by the file system,
File streams are used to flow (like a stream / river) data into/ out of a file.

QUOTE(narmer93 @ 2 Jul, 2008 - 10:30 AM) *

how can i make a text that is saved to a notepad that i create?

Below is one way.
VB

Microsoft.VisualBasic.Interaction.Shell("notepad " & filepath)


This post has been edited by AdamSpeight2008: 3 Jul, 2008 - 10:59 PM
User is offlineProfile CardPM

Go to the top of the page

narmer93
post 2 Jul, 2008 - 04:42 AM
Post #9


D.I.C Head

**
Joined: 13 Mar, 2008
Posts: 220



Thanked 1 times
My Contributions


hmm there is something wong with r code
this code worked for me
vb
 System.IO.File.WriteAllText(filepath,Me.RichTextBox1.Text)

but it only works for one time,then i have to close it and open it again and change the text in the rich textbox and press the button and the changes are made to the .txt file
=D
User is offlineProfile CardPM

Go to the top of the page

narmer93
post 2 Jul, 2008 - 04:57 AM
Post #10


D.I.C Head

**
Joined: 13 Mar, 2008
Posts: 220



Thanked 1 times
My Contributions


oh and how can i open a word document in a form that contains a rich textbox?is it possible?biggrin.gif
oh and the word dox only saves the text,it doesn't save color changes or images?why? blink.gif

This post has been edited by narmer93: 2 Jul, 2008 - 05:05 AM
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 3 Jul, 2008 - 09:03 PM
Post #11


DICula

Group Icon
Joined: 29 May, 2008
Posts: 603



Thanked 38 times

Dream Kudos: 2000
My Contributions


QUOTE(narmer93 @ 2 Jul, 2008 - 12:57 PM) *

oh and how can i open a word document in a form that contains a rich textbox?is it possible?biggrin.gif
oh and the word dox only saves the text,it doesn't save color changes or images?why? blink.gif



To save contents & styling.
vb

<name of richtextbox>.SaveFile(<Path of File>,RichTextBoxStreamType.RichText)


To Load
vb

<name of richtextbox>.LoadFile(<Path of File>,RichTextBoxStreamType.RichText)
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 11:11PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month