Search code examples
arraysvb.netlistboxopenfiledialogfile.readalllines

Load/split a .txt into multiple listboxes.?


I am trying to figure out a way to load a very large .txt file and thought if I break it into sections (Listboxes) it would load faster and be easier to manipulate with less lag. Or is there a way to OFD with a background worker? Here is how I am loading the .txt

      TextBox1.Text = ""
    OpenFileDialog1.Title = "Load File"
    OpenFileDialog1.InitialDirectory = "C:temp"
    OpenFileDialog1.ShowDialog()
    Dim path As String = OpenFileDialog1.FileName
    TextBox1.Text = path
    Dim lines() As String = IO.File.ReadAllLines(TextBox1.Text)

I can go in and mark every 1/4 of the .txt with a delimiter if that would help? I was thinking if I iterate through XX amount of lines then Next listbox etc. Maybe some form of items.count in a if not statement? My mind is going in circles please aim me in the best direction. My file is 25.MB and growing slowly. Notepad++ is the only thing handling it well ATM.

     ListBox1.Items.Add(lines(1 - 10000))

throws an error ("Outside array index or similar")

       ListBox1.Items.Add(lines(10000))

Loads the single line


Solution

  • Probably something along the lines of this. This is not 100% accurate code. But to give you an idea.

    Dim dt As New DataTable()
    Dim lines As New List(Of [String])()
    lines = New List(Of [String])(File.ReadLines(ofDialog.FileName))
    Task.Run(Function() 
    Dim options As New ParallelOptions()
    options.MaxDegreeOfParallelism = CInt(1)//Number of threads to spawn
    
    Parallel.ForEach(lines, options, Function(line) 
        dt.Rows.Add()
    End Function)
    Me.Invoke(DirectCast(Sub() listview.DataSource = dt, MethodInvoker))
    
    End Function)