llooker
llooker

Reputation: 3

Split string into 4 arrays VB.NET

So I basically have a super simple macro program use to record mouse clicks. Every time the user clicks it adds a new row into each of the 4 listboxs (Click, MouseX, Mouse Y, Time).

I want to be able to save the macros I create because I don't like re-creating them over and over...

I made a save function that saves all the data from the 4 listboxs into a text file that looks like this:

1
1
1
1
END!~
1020
1032
1134
1129
END!~
611
670
668
602
END!~
32
62
93
131
END!~

I can import this entire text file and split it by "~" that gives me array(3) like so:

    Dim bigstringy = System.IO.File.ReadAllText("C:\Users\llooker\Desktop\Macro Scripts\Macro1.txt")

    Dim strarr() As String
    strarr = bigstringy.Split("~")

I tried using

Listbox1.Items.addRange(strarr(0))
Listbox2.Items.addRange(strarr(1))

but this gives me an error....

How can I split the string into 4 separate arrays and then use those as ranges for each individual listboxs?

I'm trying to achieve this:

LISTBOX1:
    1
    1
    1
    1
    END!

LISTBOX2:
    1020
    1032
    1134
    1129
    END!

LISTBOX3:
    611
    670
    668
    602
    END!

LISTBOX4:
    32
    62
    93
    131
    END!

Upvotes: 0

Views: 302

Answers (4)

llooker
llooker

Reputation: 3

Here's my solution thanks to PT_01!

Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click

    Dim bigstringy = System.IO.File.ReadAllText("C:\Users\llooker\Desktop\Macro Scripts\Macro1.txt")

    Dim strarr() As String
    strarr = bigstringy.Split("~"c)

    ListBox1.Items.Clear()
    ListBox1.Items.AddRange(Split(strarr(0), vbCrLf))

    ListBox2.Items.Clear()
    ListBox2.Items.AddRange(Split(strarr(1), vbCrLf))

    ListBox3.Items.Clear()
    ListBox3.Items.AddRange(Split(strarr(2), vbCrLf))

    ListBox4.Items.Clear()
    ListBox4.Items.AddRange(Split(strarr(3), vbCrLf))

    ListBox2.Items.RemoveAt(0)
    ListBox3.Items.RemoveAt(0)
    ListBox4.Items.RemoveAt(0)

End Sub

Upvotes: 0

Craig Johnson
Craig Johnson

Reputation: 754

Here you go fine human:

Private Sub AddValuesToListBoxes()
    Dim count = 0
    For Each list In Split(IO.File.ReadAllText("c:\values.txt"), "~" & vbCrLf)
        CType(Controls("ListBox" & Threading.Interlocked.Increment(count)), ListBox).Items.AddRange(Split(list, vbCrLf))
    Next
End Sub

Upvotes: 0

PT_01
PT_01

Reputation: 156

Try

Listbox1.Items.addRange(Split(strarr(0), vbCrLf))
Listbox2.Items.addRange(Split(strarr(1), vbCrLf))

Upvotes: 2

Steve
Steve

Reputation: 216293

Sometime a simple old for-next loop is simpler to understand and use

Dim bigstringy = System.IO.File.ReadAllText("C:\Users\llooker\Desktop\Macro Scripts\Macro1.txt")

Dim strarr() As String
strarr = bigstringy.Split(new string() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Dim blocks = strarr.Length \ 4
for x as Integer = 0 to blocks
   Listbox1.Items.Add(strarr(x).TrimEnd("~"c))
   Listbox2.Items.Add(strarr(x + (blocks * 1)).TrimEnd("~"c))
   Listbox3.Items.Add(strarr(x + (blocks * 2)).TrimEnd("~"c))
   Listbox4.Items.Add(strarr(x + (blocks * 3)).TrimEnd("~"c))
Next

You have 4 lists and you write a block of items for each list, so splitting your text at newline allows to calculate the number of iterations required to traverse the array. At each iteration you read the blocks of data for your lists that are now evenly separated by the size of the block and add the correct set of data to your lists.

With this approach, when you write the listbox items to your file, you could remove the TILDE (~) character after the END! bookmark.
If you do so, then you could also remove that TrimEnd that I need to add 4 times just to handle this unnecessary (now) separator.

Keep in mind that, probably, this could simplified a lot if, instead of writing each listbox data sequentially, you write a line with the items at the same index in the four listboxes separating each value with a comma. For example:

1,1020,611,32
1,1032,670,62
1,1134,668,93
1,1129,602,131

Upvotes: 1

Related Questions