Aligning contents into columns.

aligning contents from txt file into columns

Page 1 of 1

5 Replies - 4257 Views - Last Post: 13 February 2010 - 01:10 PM Rate Topic: -----

#1 vglong4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-January 10

Aligning contents into columns.

Posted 09 February 2010 - 02:38 PM

Hello all. I am new to VB and I am trying to complete a project for class. I am trying to take the data from a txt file and then display it in my ListBox in proper format with columns and such. Here is the code I have now:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'will display contents of the file csvSTOCKS.txt in the list box
        Dim sr As IO.StreamReader = IO.File.OpenText("csvSTOCKS.TXT")
        Dim fmtStr As String = "{0, -15}{1, -5:C}{2, -20:C}"

        ListBox1.Items.Clear()

        Do While sr.Peek <> -1
            ListBox1.Items.Add(sr.ReadLine)
        Loop
        sr.Close()
    End Sub
End Class



The frmtStr I attempted to do is not working at all.

The data displays in my listbox but it is all grouped together like the following:

"Amgen,200,8/19/97,12.625,47.42
Delta Airlines,100,12/3/97,55.875,15.19
Novell,500,8/27/97,10.375,6.13
PPG,100,12/18/97,28.375,62.64
Timken,300,3/13/98,34.625,26.88"

Trying to get this data to align out in nice columns. |Stock| |Cost| |CurrentValue| |Profit|

Thanks for any tips.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Aligning contents into columns.

#2 Kinwang2009  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 76
  • Joined: 07-February 10

Re: Aligning contents into columns.

Posted 09 February 2010 - 09:34 PM

Hi vglong4,
This is lot easier if you use List View instead of ListBox

thanks
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Aligning contents into columns.

Posted 10 February 2010 - 07:09 AM

This is VB.NET code so I'm moving it to the VB.NET forum :)
Was This Post Helpful? 0
  • +
  • -

#4 vglong4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-January 10

Re: Aligning contents into columns.

Posted 10 February 2010 - 08:53 AM

What is the difference between List view and List box?? I am very new to VB.


View PostKinwang2009, on 09 February 2010 - 08:34 PM, said:

Hi vglong4,
This is lot easier if you use List View instead of ListBox

thanks

Was This Post Helpful? 0
  • +
  • -

#5 Aeonhack  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 25-January 10

Re: Aligning contents into columns.

Posted 13 February 2010 - 06:48 AM

ListViews are far more advanced then ListBoxes, think of it as... RichTextBox -> TextBox, XBox -> Sega, or.. Windows -> Linux (Hahaha, sorry guys I had to). ListViews support columns/subitems by default as opposed to ListBoxes which don't. Using a ListView the code would look like so:

        'I suggest you change/create these properties in the designer instead of in code.
        '-------------------------------------
        ListView1.View = View.Details
        ListView1.Columns.Add("Name", 100)
        ListView1.Columns.Add("Var2", 100)
        ListView1.Columns.Add("Date", 100)
        ListView1.Columns.Add("Var4", 100)
        ListView1.Columns.Add("Var5", 100)
        '-------------------------------------

        Do While sr.Peek <> -1
            'Parse our data, getting every value inbetween the commas.
            Dim T = sr.ReadLine.Split(",")
            'Create a new listview item, using the name as the first column value
            Dim I As New ListViewItem(T(0))
            'Add all but the last value to our ListViewItem
            I.SubItems.Add(T(1))
            I.SubItems.Add(T(2))
            I.SubItems.Add(T(3))
            'Add our new item to the listview, and append our last value.
            'This is just a preference of mine, you could add the final
            'value with the others above if you wanted to.
            ListView1.Items.Add(I).SubItems.Add(T(4))
        Loop

Was This Post Helpful? 0
  • +
  • -

#6 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Aligning contents into columns.

Posted 13 February 2010 - 01:10 PM

View Postvglong4, on 09 February 2010 - 01:38 PM, said:

Hello all. I am new to VB and I am trying to complete a project for class. I am trying to take the data from a txt file and then display it in my ListBox in proper format with columns and such. Here is the code I have now:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'will display contents of the file csvSTOCKS.txt in the list box
        Dim sr As IO.StreamReader = IO.File.OpenText("csvSTOCKS.TXT")
        Dim fmtStr As String = "{0, -15}{1, -5:C}{2, -20:C}"

        ListBox1.Items.Clear()

        Do While sr.Peek <> -1
            ListBox1.Items.Add(sr.ReadLine)
        Loop
        sr.Close()
    End Sub
End Class



The frmtStr I attempted to do is not working at all.

The data displays in my listbox but it is all grouped together like the following:

"Amgen,200,8/19/97,12.625,47.42
Delta Airlines,100,12/3/97,55.875,15.19
Novell,500,8/27/97,10.375,6.13
PPG,100,12/18/97,28.375,62.64
Timken,300,3/13/98,34.625,26.88"

Trying to get this data to align out in nice columns. |Stock| |Cost| |CurrentValue| |Profit|

Thanks for any tips.


I would use something like this

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim columns() As String = New String() {"Stock", "Cost", "CurrentValue", "Profit"}
        For Each column As String In columns
            Me.ListView1.Columns.Add(column, 96, HorizontalAlignment.Left)
        Next
        Dim fname As String = "c:\csvSTOCKS.txt"
        Dim lst As List(Of String()) = ReadCSV(fname, ",")
        Dim cnt As Integer
        For cnt = 0 To lst.Count - 1
            Dim line() As String = lst(cnt)
            Dim lvi As New ListViewItem(line(0))
            For num As Integer = 1 To line.Length - 1
                lvi.SubItems.Add(line(num))
            Next
            Me.ListView1.Items.Add(lvi)
        Next

    End Sub

    Public Function ReadCSV(ByVal fn As String, ByVal sep As String) As List(Of String())
        Dim info As List(Of String()) = New List(Of String())
        Using pars As New FileIO.TextFieldParser(fn)
            pars.SetDelimiters(sep)
            While Not pars.EndOfData
                Dim line() As String
                line = pars.ReadFields()
                info.Add(line)
            End While
        End Using
        Return info
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListView1.View = View.Details
        Me.ListView1.GridLines = True
        Me.ListView1.MultiSelect = False
    End Sub
End Class


~'J'~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1