Member Avatar for Member #958171

Hi, I want to make array with loading text file and display it in ListBox. I used this code:

Dim strFileName() As String '// String Array.
        Dim tempStr As String = "" '// temp String for result.

        strFileName = IO.File.ReadAllLines("C:\zel.txt") '// add each line as String Array.
        For Each myLine In strFileName '// loop thru Arrays.
            tempStr &= myLine & vbNewLine '// add Array and new line.\
            ListBox1.Items.Add(tempStr)
        Next

But when i click to button, in listbox adds many lines, but all lines is equal to first line of the text file. I want to display all lines in listbox. How can I do it? Thanks!

Dani AI

Generated

Nice catch by on not concatenating lines. If you just want to load the file and show each line, keep it simple and fast:

Dim path = "C:\zel.txt"
Dim lines = System.IO.File.ReadAllLines(path)

ListBox1.BeginUpdate()
ListBox1.Items.Clear()
ListBox1.Items.AddRange(lines)   'adds all lines at once
ListBox1.EndUpdate()

For your second step (fill textboxes from the selected line) you do not need to change the file format. Split on any amount of whitespace, then parse the date/time. This is more robust than hand-adding custom dividers and handles variable spaces, as hinted.

Private Sub Button2_Click(...) Handles Button2.Click
    Dim selected = TryCast(ListBox1.SelectedItem, String)
    If String.IsNullOrWhiteSpace(selected) Then Return

    Dim parts = System.Text.RegularExpressions.Regex.Split(selected.Trim(), "\s+")
    If parts.Length < 7 Then
        MessageBox.Show("Unexpected line format.")
        Return
    End If

    Dim dt = DateTime.ParseExact(parts(0) & " " & parts(1),
                                 "yyyy-MM-dd HH:mm:ss.fff",
                                 System.Globalization.CultureInfo.InvariantCulture)

    TextBox1.Text = dt.ToString("yyyy-MM-dd")
    TextBox2.Text = dt.ToString("HH:mm:ss.fff")
    TextBox3.Text = parts(2)
    TextBox4.Text = parts(3)
    TextBox5.Text = parts(4)
    TextBox6.Text = parts(5)
    TextBox7.Text = parts(6)
End Sub

To build an array of just the years from all lines:

Dim years = System.IO.File.ReadLines("C:\zel.txt").
    Where(Function(line) Not String.IsNullOrWhiteSpace(line)).
    Select(Function(line)
               Dim p = System.Text.RegularExpressions.Regex.Split(line.Trim(), "\s+")
               Dim d = DateTime.ParseExact(p(0), "yyyy-MM-dd",
                                           System.Globalization.CultureInfo.InvariantCulture)
               Return d.Year.ToString()
           End Function).
    ToArray()

Tip: if the file is large, prefer File.ReadLines (streaming) and wrap UI updates with BeginUpdate/EndUpdate to keep the UI responsive.

Recommended Answers

All 13 Replies

First time i'ma help someone on this forum. Try if this works:

Make this:

For Each myLine In strFileName '// loop thru Arrays.
            tempStr &= myLine & vbNewLine '// add Array and new line.\
            ListBox1.Items.Add(tempStr)
Next

Into this:

For Each myLine In strFileName '// loop thru Arrays.
            ListBox1.Items.Add(myLine)
Next

Checked, and works.

Member Avatar for Member #958171

Thanks, Pride! Project Worked! :)

Member Avatar for Member #958171

I have a second question:
If I want to make string from value of selected item of listbox, what I must write for 2nd button's code? My text file is like this:

2011-05-29 07:01:00.000 149     13003.00   1360.40  50388.70  52057.10
2021-05-29 07:02:00.000 149     12998.90   1369.10  50391.30  52058.80
2031-05-29 07:03:00.000 149     13005.00   1376.80  50392.20  52061.40
2041-05-29 07:04:00.000 149     13004.80   1371.20  50393.80  52062.80
2051-05-29 07:05:00.000 149     12998.90   1364.40  50397.40  52064.70
2011-05-29 07:06:00.000 149     13000.90   1364.60  50399.30  52067.10

.... and continues like it.
I want to press to 2nd button and then I want to take 2011-05-29 from selected item and put to textbox, 07:01:00.000 to 2nd textbox, 149 to 3rd textbox...
How can I do it? Thanks!

Hmm I'm not quite getting your question. Can you explain it?

If what you mean is when you click Button2, you want to make a string from the selected item in the Listbox, then look below:

    Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles MyButton.Click
        Dim MyString As String = MyListBox.Items.Item(MyListBox.SelectedIndex).ToString
        'Okay! I think you should make an adjustment to how you store your data.
        'You can always use Xml but if you don't want to atleast add some common dividers in there.

        'This is what i mean:
        2011 05 29-07:01:00.000-149-    13003.00   1360.40  50388.70  52057.10
        2021 05 29-07:02:00.000-149-    12998.90   1369.10  50391.30  52058.80
        2031 05 29-07:03:00.000-149-    13005.00   1376.80  50392.20  52061.40
        2041 05 29-07:04:00.000-149-    13004.80   1371.20  50393.80  52062.80
        2051 05 29-07:05:00.000-149-    12998.90   1364.40  50397.40  52064.70
        2011 05 29-07:06:00.000-149-    13000.90   1364.60  50399.30  52067.10
        'So I added some dividers so I can split it("-").
        'I know I could have split it with a " " (Space) but I don't like using that
        Dim MyStringPart1 as string = MyString.Split("-").GetValue(0)
        Dim MyStringPart2 as string = MyString.Split("-").GetValue(1)
        Dim MyStringPart3 as string = MyString.Split("-").GetValue(2)
        MyTextBox1.Text = MyStringPart1
        MyTextBox2.Text = MyStringPart2
        MyTextBox3.Text = MyStringPart3
    End Sub

If that was your question.
Also, I suggest you post new questions in new threads, you'll get more hits.

commented: great! +0
Member Avatar for Member #958171

thanks very much, worked!

No problem :)

Member Avatar for Member #958171

OK, how I can get all first column (2011,2021,2031,2041,2051,2011...) in

        2011 05 29-07:01:00.000-149-    13003.00   1360.40  50388.70  52057.10
        2021 05 29-07:02:00.000-149-    12998.90   1369.10  50391.30  52058.80
        2031 05 29-07:03:00.000-149-    13005.00   1376.80  50392.20  52061.40
        2041 05 29-07:04:00.000-149-    13004.80   1371.20  50393.80  52062.80
        2051 05 29-07:05:00.000-149-    12998.90   1364.40  50397.40  52064.70
        2011 05 29-07:06:00.000-149-    13000.90   1364.60  50399.30  52067.10
        .......
        .......

and make an array from those strings(2011,2021,2031,2041,2051,2011)?

Member Avatar for Member #958171

OK, how I can get all first column (2011,2021,2031,2041,2051,2011...) in

        2011 05 29-07:01:00.000-149-    13003.00   1360.40  50388.70  52057.10
        2021 05 29-07:02:00.000-149-    12998.90   1369.10  50391.30  52058.80
        2031 05 29-07:03:00.000-149-    13005.00   1376.80  50392.20  52061.40
        2041 05 29-07:04:00.000-149-    13004.80   1371.20  50393.80  52062.80
        2051 05 29-07:05:00.000-149-    12998.90   1364.40  50397.40  52064.70
        2011 05 29-07:06:00.000-149-    13000.90   1364.60  50399.30  52067.10
        .......
        .......

and make an array from those strings(2011,2021,2031,2041,2051,2011)?

Member Avatar for Member #958171

OK, how I can get all first column (2011,2021,2031,2041,2051,2011...) in

        2011 05 29-07:01:00.000-149-    13003.00   1360.40  50388.70  52057.10
        2021 05 29-07:02:00.000-149-    12998.90   1369.10  50391.30  52058.80
        2031 05 29-07:03:00.000-149-    13005.00   1376.80  50392.20  52061.40
        2041 05 29-07:04:00.000-149-    13004.80   1371.20  50393.80  52062.80
        2051 05 29-07:05:00.000-149-    12998.90   1364.40  50397.40  52064.70
        2011 05 29-07:06:00.000-149-    13000.90   1364.60  50399.30  52067.10
        .......
        .......

and make an array from those strings(2011,2021,2031,2041,2051,2011)?

Member Avatar for Member #958171

OK, how I can get all first column (2011,2021,2031,2041,2051,2011...) in

        2011 05 29-07:01:00.000-149-    13003.00   1360.40  50388.70  52057.10
        2021 05 29-07:02:00.000-149-    12998.90   1369.10  50391.30  52058.80
        2031 05 29-07:03:00.000-149-    13005.00   1376.80  50392.20  52061.40
        2041 05 29-07:04:00.000-149-    13004.80   1371.20  50393.80  52062.80
        2051 05 29-07:05:00.000-149-    12998.90   1364.40  50397.40  52064.70
        2011 05 29-07:06:00.000-149-    13000.90   1364.60  50399.30  52067.10
        .......
        .......

and make an array from those strings(2011,2021,2031,2041,2051,2011)?

Using split function with delimiter as vbSpace get the contenets into array load the first item of array into New array list or use wherever u want to..

Member Avatar for Member #958171

please, explain with code :)

Sorry man. :P I was busy playing games. Anyways here:
First of all lets add some dividers(".") in there.

        2011.05.29-07:01:00.000-149-    13003.00   1360.40  50388.70  52057.10
        2021.05.29-07:02:00.000-149-    12998.90   1369.10  50391.30  52058.80
        2031.05.29-07:03:00.000-149-    13005.00   1376.80  50392.20  52061.40
        2041.05.29-07:04:00.000-149-    13004.80   1371.20  50393.80  52062.80
        2051.05.29-07:05:00.000-149-    12998.90   1364.40  50397.40  52064.70
        2011.05.29-07:06:00.000-149-    13000.90   1364.60  50399.30  52067.10

Public Class MyClass

    Public Sub GetYears()
        Dim MyDate As String() = My.Computer.FileSystem.ReadAllText("MyData.txt").Split(Environment.NewLine)

        Dim MyYears As New List(Of String)

        For Each Line As String In MyDate
            Dim MyYear As String = Line.Split("-").GetValue(0).Split(".").GetValue(0)
            MyYears.Add(MyYear)
        Next

        'MyYears is a list of the years.
    End Sub

End Class

Any more questions? :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.