VB.Net Search In A Multidimensional Array

How To Find Item Position In A 2D Array Using VB.Net

vb.net search in 2d array



In this VB.NET Tutorial we will see How To Search And Find A Value Position Inside A Multidimensional Array Using For Loop To Go through all Array Items And a TextBox To Enter The Value You want to search And Display The Positions Inside A RichTextBox On A Button Click Event In Visual Basic.Net Programming Language And Visual  Studio Editor.




Project Source Code:


Public Class _2D_Array_Create_Search_Display

   ' create 2d array
    Dim data(,) As String

    Private Sub _2D_Array_Create_Search_Display_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' add data to the 2d array

        data = New String(,) {
                               {"1", "A", "B", "CD"}, {"2", "C", "D", "EF"},
                               {"3", "E", "F", "KL"}, {"4", "K", "L", "MN"},
                               {"5", "M", "N", "MN"}, {"6", "G", "R", "HS"},
                               {"7", "H", "S", "ZW"}, {"8", "Z", "W", "MN"}
                             }

        ' display 2d array data
        For i As Integer = 0 To data.GetLength(0) - 1 Step +1

            For j As Integer = 0 To data.GetLength(1) - 1 Step +1

                RichTextBox1.Text += data(i, j) + " - "

            Next j

            RichTextBox1.Text += vbNewLine

        Next i


    End Sub

' button search
    Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click

' clear the RichTextBox
        RichTextBox2.Text = ""

        ' search item position in 2D array data
        For i As Integer = 0 To data.GetLength(0) - 1 Step +1

            For j As Integer = 0 To data.GetLength(1) - 1 Step +1

                If TextBox1.Text.Equals(data(i, j)) Then

                    RichTextBox2.Text += "POSITION [ " + i.ToString() + " , " + j.ToString() + " ]"
                    RichTextBox2.Text += vbNewLine

                End If


            Next j


        Next i

    End Sub
End Class


OutPut:

get item position in 2d array using vb.net




Share this

Related Posts

Previous
Next Post »