Write a VB program to load data from the given text file, and store in a Structure Array. Then, display the data in a Data Grid View.
The program should allow user to search for customer using a Combo Box of country, with the options of “Malaysia”, “England”, and “Japan”.
STEP 1: Load the data from the given text file into a Structure Array when Form.Load. Then, the all data in the DataGridView.
STEP 2: Click the Search button, then rewrite the query to only display the customer belongs to the selected country.
Interface:
Country: (ComboBox)
Search(Button)
(DataGridView)
(TextFile)
FirstName LastName Gender Country
x y M Malaysia
a b F England
i j M Japan
Imports System.IO
Public Class CountryProjectForm
    Structure PersonDetails
        Public _FirstName As String
        Public _LastName As String
        Public _Gender As String
        Public _Country As String
        Public Sub New(ByVal FirstName As String, ByVal LastName As String, ByVal Gender As String, ByVal Country As String)
            _FirstName = FirstName
            _LastName = LastName
            _Gender = Gender
            _Country = Country
        End Sub
        Public Property FirstName() As String
            Get
                Return _FirstName
            End Get
            Set(ByVal value As String)
                _FirstName = value
            End Set
        End Property
        Public Property LastName() As String
            Get
                Return _LastName
            End Get
            Set(ByVal value As String)
                _LastName = value
            End Set
        End Property
        Public Property Gender() As String
            Get
                Return _Gender
            End Get
            Set(ByVal value As String)
                _Gender = value
            End Set
        End Property
        Public Property Country() As String
            Get
                Return _Country
            End Get
            Set(ByVal value As String)
                _Country = value
            End Set
        End Property
    End Structure
    Private person_Details As New List(Of PersonDetails)
    Private Sub CountryProjectForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim FilePath As String = "Persons.txt"
        Using reader As StreamReader = New StreamReader(FilePath)
            ' Read first line
            reader.ReadLine()
            For i As Integer = 0 To 2
                Dim values() = reader.ReadLine().Split(" ")
                Dim person_Detail As New PersonDetails(values(0), values(1), values(2), values(3))
                person_Details.Add(person_Detail)
            Next
            reader.Close()
        End Using
        dgvPersonDetails.DataSource = person_Details
        dgvPersonDetails.Refresh()
        cbCountries.SelectedIndex = 1
    End Sub
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim query = From person In person_Details
        Where person.Country = cbCountries.SelectedItem.ToString()
        dgvPersonDetails.DataSource = query.ToList()
    End Sub
End Class
Comments