2 Replies - 2020 Views - Last Post: 07 July 2008 - 09:51 AM Rate Topic: -----

#1 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Displaying Listview Headers in .txt File

Posted 06 July 2008 - 08:31 PM

I have a listview that has five columns with a heading at the top of each column. I want the user to be able to click the Save Button and have the contents of the listview along with the corresponding column headings saved to a .txt file. I currently have code (below) that saves the contents of the listview to the .txt file with no prolems except there are no column headers. I have been expirimenting with inserting another writeline in with the static column headings before I start my loop but have been unable to get any results. I have also been expirimenting with trying to call the listview collection of headers. It seems that there should be some easy way to call the collection before I start my loop or refer to it. I was trying something like this:

Headers.Add(ListViewSummary.Columns(i).Text)




Here is the code that I am using to save the Listview contents to the .txt file:


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

		Dim filepath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\listview.txt" 
		Dim outputstream As New IO.StreamWriter(filepath) 

		Dim s As Short 
		Dim newstr(5) As String 

		Dim I As Integer 

		Dim Text1 As String 
		Dim Text2 As String 
		Dim Text3 As String 
		Dim Text4 As String 
		Dim Text5 As String 
		Dim ItemCount As Integer 

		ItemCount = Me.ListViewSummary.Items.Count 

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

			Text1 = newstr(0) 
			Text2 = newstr(1) 
			Text3 = newstr(2) 
			Text4 = newstr(3) 
			Text5 = newstr(4) 

			outputstream.WriteLine(Text1 & " / " & Text2 & " / " & Text3 & " / " & Text4 & " / " & Text5) 
		Next I 

		outputstream.Close() 
	End Sub 



Any suggestions are appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Listview Headers in .txt File

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: Displaying Listview Headers in .txt File

Posted 06 July 2008 - 11:23 PM

Here is a small little example of how you get those column names and can use it for writing to a file...


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim col As ColumnHeader
        Dim columnnames As String = ""

        ' Loop through the column headers of the listview
        For Each col In ListView1.Columns
            ' If this is the first column, set the variable to its caption
            ' Otherwise concatenate the columns preceeded with a comma
            If String.IsNullOrEmpty(columnnames) Then
                columnnames = col.Text
            Else
                columnnames &= ", " & col.Text
            End If
        Next

        ' Here we show the column names and this is where you can write them to file
        MessageBox.Show(columnnames)
End Sub



As you see we simply loop through the columns of our listbox, read each columnheader text value and concatenate them into a string. At the end you can write this to the text file anyway you like.

Enjoy!

"At DIC we be columnheader looping code ninjas... we also can do headers with empty beer cans!" :snap:
Was This Post Helpful? 1

#3 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Displaying Listview Headers in .txt File

Posted 07 July 2008 - 09:51 AM

Thanks Martyr! I am off to find some empty beer cans. :D I don't know if any other VB.Net Newbie needs assistance with this but I took Martyrs example and the added a writeline instead of a messagebox and a pipe instead of a comma between columns. Other than spacing the inputs to line everything up it works great. Here is the final code which you can attach to a menu bar button, menu item or a command button to save the contents and headers of a list view box to a .txt file (Thanks AS'08 for the help on the save path!) You will want to make some slight modifications to the textbox1, 2, 3 string to reflect what your programmatic needs are.


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

'Setting the path for the .txt file to be saved

		Dim filepath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\listview.txt"
		Dim outputstream As New IO.StreamWriter(filepath)

		Dim col As ColumnHeader
		Dim columnnames As String = ""

		'Loop through the column headers of the listview
		
For Each col In ListViewSummary.Columns
			' If this is the first column, set the variable to its caption
			' Otherwise concatenate the columns preceeded with a comma
			If String.IsNullOrEmpty(columnnames) Then
				columnnames = col.Text
			Else
				columnnames &= " | " & col.Text
			End If
		Next

		'Write the column names to file
		
		outputstream.WriteLine(columnnames)

		Dim s As Short
		Dim newstr(5) As String

		Dim I As Integer
		
		'User textbox entries

		Dim Textbox1Input As String
		Dim Textbox2Input As String
		Dim Textbox3Input  As String
		Dim Textbox4Input  As String
		Dim Textbox5Input  As String
		Dim ItemCount As Integer
	  
		'Loop through the input items

		ItemCount = Me.ListViewSummary.Items.Count

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

			Next

			Textbox1Input  = newstr(0)
			Textbox2Input  = newstr(1)
			Textbox3Input  = newstr(2)
			Textbox4Input  = newstr(3)
			Textbox5Input  = newstr(4)

			'Format the output

			outputstream.WriteLine(Textbox1Input  & " |  " & Textbox2Input  & "  |  " &  Textbox3Input  & "  |  " & Textbox4Input  & "  |  " & Textbox5Input )

		Next I

		outputstream.Close()
	End Sub



This post has been edited by gram999: 07 July 2008 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1