A Program in Linear Search Using Visual Basic 2008 (version 9.0)
                          By Ariel T. Sumagaysay, MSCS

A. Form Design

Design the form shown below using the Visual Basic 2008:




B. Components

      The following are the components of the program: 1 Form, 1 GroupBox, 4 Labels,
      1 ListBox, 5 TextBoxes and 5 Buttons.
2


C. Code Listings

'LINEAR SEARCH
'PROGRAM BY ARIEL T. SUMAGAYSAY
'COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY
'WEST NEGROS UNIVERSITY
'DECEMBER 15, 2008

Public Class Form1

    'Variables
    Dim nNumberOfElements As Integer
    Dim sNumbers As String
    Dim nFind As Integer
    Dim n As Integer
    Dim x As Integer
    Dim s As String

    'One Dimensional Array
    Dim aInputSet() As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        'Setting Buttons - controls
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False

        TextBox1.Enabled = True
        TextBox6.Enabled = False
        TextBox7.Enabled = False

        TextBox1.Text = " "
        TextBox2.Text = " "
        TextBox3.Text = " "
        TextBox6.Text = " "
        TextBox7.Text = " "
        ListBox1.Items.Clear()

        'Initialize variables
        nNumberOfElements = 0
        n = 0
        x = 0
        s = " "
        sNumbers = " "        'Set string of numbers to NULL
        nFind = 0

        'Set the Number of Elements
        ReDim aInputSet(0)
    End Sub
3

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        'Re - setting Buttons
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = True

        TextBox1.Enabled = True
        TextBox6.Enabled = False
        TextBox7.Enabled = False

        TextBox1.Text = " "
        TextBox2.Text = " "
        TextBox3.Text = " "
        TextBox6.Text = " "
        TextBox7.Text = " "
        ListBox1.Items.Clear()

        'Initialize variables
        nNumberOfElements = 0
        n = 0
        x = 0
        s = " "
        sNumbers = " "        'Set string of numbers to NULL
        nFind = 0

        'Set the Number of Elements
        ReDim aInputSet(0)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        'Terminate Program
        End

    End Sub
    Private Sub bubblesort()
        'Sort the elements using bubblesort
        Static a As Integer
        Static b As Integer
        Static temp As Integer
        Static sHereNumbers As String

        'initialize variables
        a = 0
        b = 0
        temp = 0
        sHereNumbers = " "

        For a = 1 To n
            For b = 1 To n - 1
4

                      If (aInputSet(b) > aInputSet(b + 1)) Then
                          temp = aInputSet(b)
                          aInputSet(b) = aInputSet(b + 1)
                          aInputSet(b + 1) = temp
                      End If
               Next
        Next

        'display list
        For a = 1 To n
             sHereNumbers = sHereNumbers + Str(aInputSet(a)) & " "
             TextBox3.Text = sHereNumbers
        Next

        'Enable Search button
        Button3.Enabled = True
        TextBox7.Enabled = True
    End Sub

    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
        'Setting Buttons
        Button4.Enabled = True
        Button5.Enabled = False

       TextBox6.Enabled = True
       TextBox1.Enabled = False

       'Get number of elements
       nNumberOfElements = CInt(TextBox1.Text)
       n = nNumberOfElements

        ReDim aInputSet(n)

        'set command button label
        s = Format(1, "0#")
        Button4.Text = "&ENTER ELEMENT " + s
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
        Static y As Integer
        y = 0

        'Enter numbers
        If TextBox6.Text <> " " Then
            x = x + 1
            y = x

               s = Format(y + 1, "0#")
               Button4.Text = "&ENTER ELEMENT " + s
               aInputSet(x) = CInt(TextBox6.Text)

               If x = n Then
                   Button4.Enabled = False
                   TextBox6.Enabled = False
                   Button4.Text = "&ENTER ELEMENT"
5


                  Call bubblesort()
              End If

            sNumbers = sNumbers & TextBox6.Text & " "
            TextBox2.Text = sNumbers
            TextBox6.Text = " "
        End If


    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
        Static z As Integer
        Static bFind As Boolean

        'Setting Buttons & Initialization
        Button2.Enabled = True
        bFind = False
        ListBox1.Items.Clear()

        'Searching
        nFind = CInt(TextBox7.Text)

        For z = 1 To n
             If nFind = aInputSet(z) Then
                 ListBox1.Items.Add("Number is found on location " & Str(z))
                 bFind = True
             End If
        Next

        If bFind = False Then
            ListBox1.Items.Add(" Number is NOT FOUND on the LIST! ")
        End If

    End Sub
End Class

Linear Search Program in Visual Basic 2008

  • 1.
    A Program inLinear Search Using Visual Basic 2008 (version 9.0) By Ariel T. Sumagaysay, MSCS A. Form Design Design the form shown below using the Visual Basic 2008: B. Components The following are the components of the program: 1 Form, 1 GroupBox, 4 Labels, 1 ListBox, 5 TextBoxes and 5 Buttons.
  • 2.
    2 C. Code Listings 'LINEARSEARCH 'PROGRAM BY ARIEL T. SUMAGAYSAY 'COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY 'WEST NEGROS UNIVERSITY 'DECEMBER 15, 2008 Public Class Form1 'Variables Dim nNumberOfElements As Integer Dim sNumbers As String Dim nFind As Integer Dim n As Integer Dim x As Integer Dim s As String 'One Dimensional Array Dim aInputSet() As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Setting Buttons - controls Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False TextBox1.Enabled = True TextBox6.Enabled = False TextBox7.Enabled = False TextBox1.Text = " " TextBox2.Text = " " TextBox3.Text = " " TextBox6.Text = " " TextBox7.Text = " " ListBox1.Items.Clear() 'Initialize variables nNumberOfElements = 0 n = 0 x = 0 s = " " sNumbers = " " 'Set string of numbers to NULL nFind = 0 'Set the Number of Elements ReDim aInputSet(0) End Sub
  • 3.
    3 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'Re - setting Buttons Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False Button5.Enabled = True TextBox1.Enabled = True TextBox6.Enabled = False TextBox7.Enabled = False TextBox1.Text = " " TextBox2.Text = " " TextBox3.Text = " " TextBox6.Text = " " TextBox7.Text = " " ListBox1.Items.Clear() 'Initialize variables nNumberOfElements = 0 n = 0 x = 0 s = " " sNumbers = " " 'Set string of numbers to NULL nFind = 0 'Set the Number of Elements ReDim aInputSet(0) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Terminate Program End End Sub Private Sub bubblesort() 'Sort the elements using bubblesort Static a As Integer Static b As Integer Static temp As Integer Static sHereNumbers As String 'initialize variables a = 0 b = 0 temp = 0 sHereNumbers = " " For a = 1 To n For b = 1 To n - 1
  • 4.
    4 If (aInputSet(b) > aInputSet(b + 1)) Then temp = aInputSet(b) aInputSet(b) = aInputSet(b + 1) aInputSet(b + 1) = temp End If Next Next 'display list For a = 1 To n sHereNumbers = sHereNumbers + Str(aInputSet(a)) & " " TextBox3.Text = sHereNumbers Next 'Enable Search button Button3.Enabled = True TextBox7.Enabled = True End Sub Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click 'Setting Buttons Button4.Enabled = True Button5.Enabled = False TextBox6.Enabled = True TextBox1.Enabled = False 'Get number of elements nNumberOfElements = CInt(TextBox1.Text) n = nNumberOfElements ReDim aInputSet(n) 'set command button label s = Format(1, "0#") Button4.Text = "&ENTER ELEMENT " + s End Sub Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Static y As Integer y = 0 'Enter numbers If TextBox6.Text <> " " Then x = x + 1 y = x s = Format(y + 1, "0#") Button4.Text = "&ENTER ELEMENT " + s aInputSet(x) = CInt(TextBox6.Text) If x = n Then Button4.Enabled = False TextBox6.Enabled = False Button4.Text = "&ENTER ELEMENT"
  • 5.
    5 Call bubblesort() End If sNumbers = sNumbers & TextBox6.Text & " " TextBox2.Text = sNumbers TextBox6.Text = " " End If End Sub Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Static z As Integer Static bFind As Boolean 'Setting Buttons & Initialization Button2.Enabled = True bFind = False ListBox1.Items.Clear() 'Searching nFind = CInt(TextBox7.Text) For z = 1 To n If nFind = aInputSet(z) Then ListBox1.Items.Add("Number is found on location " & Str(z)) bFind = True End If Next If bFind = False Then ListBox1.Items.Add(" Number is NOT FOUND on the LIST! ") End If End Sub End Class