I have a datagridview that the user can change to various rows and columns (its a grid of squares)

When the user presses a button, it generates a list of coordinates of all the squares that are black.
That is obviously generated in order.
I want it to be mixed up, so that the coordinates arent in order: (2,1) (1,3) (2,2) not (1,3) (2,1) (2,2)

Here is the code that I have so far:

 Public Sub GenerateCoordinates()

        temporary = 0
        For index As Integer = 0 To Grid.RowCount - 1
            For index2 As Integer = 0 To Grid.ColumnCount - 1
                If (Grid.Rows(index).Cells(index2).Style.BackColor = Color.Black) Then
                    temporary += 1
                    If (Len("(" & index2 + 1 & "," & index + 1 & ")") > 5) Then
                        If (Len("(" & index2 + 1 & "," & index + 1 & ")") = 6) Then
                            builder.Append("(" & index2 + 1 & "," & index + 1 & ")  ")
                        End If
                        If (Len("(" & index2 + 1 & "," & index + 1 & ")") = 7) Then
                            builder.Append("(" & index2 + 1 & "," & index + 1 & ") ")
                        End If
                    Else
                        builder.Append("(" & index2 + 1 & "," & index + 1 & ")   ")
                    End If

                End If

                If (temporary = PrinterDialog.CoordinateRows.Value) Then
                    temporary = 0
                    builder.Append(vbCrLf)
                End If
            Next
        Next
        Form2.CoordinateList.Text = builder.ToString()
    End Sub

All of those nested IFs are a replacement for tabs.

Instead of using string builder, a string array, declared to the size you need and initialized to null strings, will allow to assign the coordinates in which ever order you need. You could even go so far as to use a function to assign random indexes. Then use the Join function to put them all back into one string. The count property of the array will give you the number of coordinates you'll be printing.

On a side note. If you declare a temporary variable to hold the string of the individual coordinates(Dim TempStr as String = "(" & index2 + 1 & "," & index + 1 & ")"), then use that in your code, it will greatly clean up your code and make it much easier to understand.

Also instead of padding your string with spaces to maintain you length if you use the PadLeft method to pad the string of the individual numbers to a length of 2, the length will automatically be maintained at 7(Dim TempStr as String = "(" & (index2 + 1).ToString.PadLeft(2, "0"c) & "," & (index + 1).ToString.PadLeft(2, "0"c) & ")"

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.