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!

ziyaddinsadigov
Deleted Member

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.

Edited 4 Years Ago by Pride

Thanks, Pride! Project Worked! :)

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.

Edited 4 Years Ago by Pride

Comments
great!

thanks very much, worked!

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)?

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)?

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)?

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..

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

This article has been dead for over six months. Start a new discussion instead.