Member Avatar for khaos64

I am very new to VB.NET, I want to create a Forms Application to load a text file that has lets say 50 lines, some of the lines are identical and those are grouped together and some same format but different make up. Have it loads it into a text box in the form, to see - but I want another button to randomize all the lines and then show that in the box, and then have a Save button to overwrite the orginal. Example of .txt.

line of text aa
line of text aa
line of text bb
line of text cc
line of text cc
line of text cc
line of text dd
line of text ee
line of text ee
line of text ff
line of text ff
line of text ff

Now when the list is randomized I want the identical lines to stay together, so:

line of text dd
line of text ff
line of text ff
line of text ff
line of text aa
line of text aa
line of text ee
line of text ee
line of text cc
line of text cc
line of text cc
line of text bb

So far, I have been able to Load the list into a text box on the form using openfiledialog,(which I can manually edit), clear it, and save over it. But have no idea of going about creating a button to do all the stuff I said above, here's what I have. Which I have put together by just piecing together different tutorials on youtube and a little experimenting.

Public Class Shuffle


    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim FileData As New System.IO.StreamReader(OpenFileDialog1.OpenFile)
        ListBox.Text = FileData.ReadToEnd
        FileData.Close()
    End Sub

    Private Sub Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Browse.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub Random_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Random.Click

    End Sub

    Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
        ListBox.Clear()
    End Sub

    Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
        Dim WriteData As New System.IO.StreamWriter(OpenFileDialog1.FileName)
        WriteData.Write(ListBox.Text)
        WriteData.Close()

    End Sub
End Class

Any/All help is greatly appreciated.

Member Avatar for khaos64

Well for an update, I have changed it from loading into a textbox to a listbox. I have found a way to randomize the list but not sure how to keep the identical lines grouped and randomize. Also, I want to implement the dragdrop option into the listbox. I have found plenty of things on the web about dropping a text file from Windows Explorer and it loading the filepath and file name, but I can't find anything that loads the data of the text file into the Listbox. From what I've been looking around and what not, it seems, I would somehow get the data into an array list and then have the array list add each item to the listbox. But can't figure out how do do it.

Code of current randomize(doesn't keep identical lines grouped):

Private Sub Random_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Random.Click
        Dim Random As New System.Random

        ListBox.BeginUpdate()

        Dim ArrayList As New System.Collections.ArrayList(ListBox.Items)

        ListBox.Items.Clear()

        While ArrayList.Count > 0

            Dim Index As System.Int32 = Random.Next(0, ArrayList.Count)

            ListBox.Items.Add(ArrayList(Index))

            ArrayList.RemoveAt(Index)

        End While

        ListBox.EndUpdate()
        Label4.Text = "Total: " & ListBox.Items.Count
    End Sub

What I was trying with the DragDrop(Doesn't work at all) also is enabled in properties:

Private Sub ListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragDrop

        Dim Data As New ArrayList()


        Data = e.Data.GetData(Text)
        For Each obj As Object In Data
            ListBox.Items.Add(obj)
        Next

        Label4.Text = "Total: " & ListBox.Items.Count


    End Sub


    Private Sub ListBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If

    End Sub

If your loading the items into array list then sort the array to group the items.

Member Avatar for khaos64

If your loading the items into array list then sort the array to group the items.

Update - Figured out DragDrop

Private Sub ListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragDrop

        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim filePaths As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            For Each fileLoc As String In filePaths
                ' Code to read the contents of the text file
                If File.Exists(fileLoc) Then
                    Using tr As TextReader = New StreamReader(fileLoc)
                        Dim a As String = tr.ReadToEnd
                        Dim b As String() = a.Split(vbNewLine)
                        ListBox.Items.AddRange(b)
                    End Using
                End If

            Next fileLoc
        End If


        Label4.Text = "Total: " & ListBox.Items.Count

    End Sub


    Private Sub ListBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If

    End Sub

I don't know, and can't find anything that will help me get specific items into its own array, when what I am identifying the items is by 3-4 numbers, but always located in the same spot(i.e. xxx123xxx or xxx3456xxx). so i can shuffle the arrays then add them back to the list.

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.