How to randomize an array in VB.net

Do you want to randomize the items in an array with VB.net? Add these 2 functions below to your code to get it working. The reason there’s 2 functions you must add is because the first function is using the second function to make it all work.

To use it, you just type: Shuffle(YourArray, 5)

The number is just how many times it’s going to shuffle the array, you can use whatever you want here.

Here is the code for the 2 functions:

Public Function Shuffle(ByRef arrayToBeShuffled As Array, ByVal numberOfTimesToShuffle As Integer)
        Dim rndPosition As New Random(DateTime.Now.Millisecond)
        For i As Integer = 1 To numberOfTimesToShuffle
            For i2 As Integer = 1 To arrayToBeShuffled.Length
                Swap(arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)), arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)))
            Next i2
        Next i
        Return ("")
End Function

Public Function Swap(ByRef arg1 As Object, ByRef arg2 As Object)
        Dim strTemp As String
        strTemp = arg1
        arg1 = arg2
        arg2 = strTemp
        Return ("")
End Function

Remove duplicates from array in VB.net

Remove duplicates from array in VB.net

Here’s a function that will remove duplicate items in a array. The code is for VB.net.

The code:

    Function RemoveDuplicates(ByVal initialArray As String())
        Dim i As Integer
        Dim j As Integer
        Dim newArray(0) As String

        For i = 0 To UBound(initialArray)
            For j = 0 To UBound(initialArray)
                If Not initialArray(i) = "" Then
                    If Not j = i Then
                        If initialArray(i) = initialArray(j) Then
                            initialArray(j) = ""
                        End If
                    End If
                End If
            Next
        Next

        j = 0
        For i = 0 To UBound(initialArray)
            If Not initialArray(i) = "" Then
                ReDim Preserve newArray(j)
                newArray(j) = initialArray(i)
                j += 1
            End If
        Next

        Return newArray

    End Function

VB.net – Each listview line in a different color

This is how to make each line in a listview have a different color.

The listview in this example is named “lv”.

                Dim myRow As ListViewItem
                Dim RowCount As Integer = 0
                For Each myRow In lv.Items
                    If RowCount Mod 2 = 0 Then
                        myRow.BackColor = Color.Silver
                    Else
                        myRow.BackColor = Color.LightGray
                    End If
                    RowCount = RowCount + 1
                Next

Download website source code with VB.net

This is a simple guide about how to download a websites source code with Visual Basic.

Imports System.Net
Public Class Form1
    Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click

        Dim client As WebClient = New WebClient()
        client.Headers("User-Agent") = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"
        TextBox1.Text = client.DownloadString("http://example.com/")

    End Sub
End Class

This piece of code will put the source code from example.com in a textbox called TextBox1.