Home

File-Based Applications:
Bethesda Car Rental

 

Introduction

Imagine you get a contract or an assignment to create an application used to process business transactions for a car rental company. In this case, we will call it Bethesda Car Rental. If you decide to use then .NET Framework to develop the application, you have various options. You can use file processing for a file-based database. You can proceed with XML to save company files in a format that can be accessed by different types of applications, including the World Wide Web. Or you can create a formal database using Microsoft SQL Server, Borland/Corel Paradox, or MS Access, etc. In this example, we will create our application using regular files.

This application assumes that you have some knowledge or a basic understanding of file processing and serialization as it is featured in the .NET Framework but we will be as clear as possible with this exercise and its different parts.

 

Practical Learning Practical Learning: Processing Cars 

  1. Start Microsoft Visual Basic .NET or Visual Studio .NET and create a new Windows Application named BCR2
  2. In the Solution Explorer, right-click Form1 and click Rename
  3. Type Switchboard.vb and press Enter twice twice to display the first form

Employees

An employee will be anybody who works for this company, whether the person is part of the management or is one of those who process transactions. In a formal company, various pieces of information are provided for each employee. In our example, we will get only the strict minimum.

 

Practical Learning Practical Learning: Creating Employees

  1. To add a new form to the application, on the main menu, click Project -> Add Windows Form...
  2. Set the Name to Employees and press Enter
  3. From the Toolbox, add a ListView to the form
  4. While the new list view is still selected, in the Properties window, click the ellipsis button of the Columns field and create the columns as follows:
     
    (Name) Text Width
    colFirstName First Name 80
    colLastName Last Name 80
    colTitle Title 200
    colGender Gender 80
  5. Design the form as follows: 
     
     
    Control Text Name Other Properties
    ListView   lvwEmployees Anchor: Top, Bottom, Left, Right
    FullRowSelect: True
    GridLines: True
    View: Details
    Button New Employee btnNewEmployee  
    Button Close btnClose  
  6. To add another form to the application, on the main menu, click Project . Add Windows Forms...
  7. Set the Name to NewEmployee and press Enter
  8. Design the form as follows: 
     
     
    Control Text Name Properties
    Label First Name:    
    TextBox   txtFirstName Modifiers: public
    Label Last Name:    
    TextBox   txtLastName Modifiers: public
    Label Title:    
    TextBox   txtTitle Modifiers: public
    Label Gender:    
    ComboBox   cboGenders Modifiers: public
    Items: Female
    Male
    Unknown
    Button Add btnAdd DialogResult: OK
    Button Cancel btnCancel DialogResult: Cancel
    Form     AcceptButton: btnAdd
    CancelButton: btnClose
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  9. To add a new class to the project, on the main menu, click Project -> Add Class...
  10. Set the Class Name to Employee and click Open
  11. Change the Employee.vb file as follows:
     
    <Serializable()> Public NotInheritable Class Employee
        Public FirstName As String
        Public LastName As String
        Public Title As String
        Public Gender As String
    
        Public Sub New()
            FirstName = "Unknown"
            LastName = "Unknown"
            Title = "N/A"
            Gender = "Unknown"
        End Sub
    
        Public Sub New(ByVal fname As String, ByVal lname As String, _
                       ByVal title As String, ByVal sex As String)
            FirstName = fname
            LastName = lname
            title = title
            Gender = sex
        End Sub
    End Class
  12. In the Solution Explorer, right-click the References node and click Add Reference...
  13. In the .NET property page of the Add Reference dialog box, select System.Runtime.Serialization.Formatters.Soap and click Select
     
    Add Reference
  14. Click OK
  15. Return to the Employees form. Right-click it and click View Code
  16. In the top section of the file, type:
     
    Imports System.IO
    Imports System.Runtime.Serialization.Formatters.Soap
    
    Public Class Employees
        Inherits System.Windows.Forms.Form
    
        Private lstEmployees As ArrayList
    
  17. Just before the End Class line, create a sub procedure as follows:
     
    Private Sub ShowEmployees()
            Dim bcrSoap As SoapFormatter = New SoapFormatter
            Dim strFilename As String = "Employees.bcr"
    
            If File.Exists(strFilename) Then
    Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                Dim lstEmpl As ArrayList = bcrSoap.Deserialize(bcrStream)
                bcrStream.Close()
    
                Dim empl As Employee
    
                lvwEmployees.Items.Clear()
    
                For i As Integer = 0 To lstEmpl.Count - 1
                    empl = lstEmpl(i)
                    Dim lviEmployee As ListViewItem = New ListViewItem(empl.FirstName)
                    lviEmployee.Font = New Font("Georgia", 8, FontStyle.Bold)
    
                    If i Mod 2 = 0 Then
                        lviEmployee.BackColor = Color.Blue
                        lviEmployee.ForeColor = Color.White
                    Else
                        lviEmployee.BackColor = Color.LightBlue
                        lviEmployee.ForeColor = Color.Blue
                    End If
    
                    lviEmployee.SubItems.Add(empl.LastName)
                    lviEmployee.SubItems.Add(empl.Title)
                    lviEmployee.SubItems.Add(empl.Gender)
                    lvwEmployees.Items.Add(lviEmployee)
                Next
            End If
    End Sub
  18. In the Class Name combo box, select (Employees Events)
  19. In the Method Name combo box, select Load and implement the event as follows:
     
    Private Sub Employees_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            lstEmployees = New ArrayList
            Dim bcrSoap As SoapFormatter = New SoapFormatter
            Dim strFilename As String = "Employees.bcr"
    
            If File.Exists(strFilename) Then
    Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
                lstEmployees = bcrSoap.Deserialize(bcrStream)
                bcrStream.Close()
            End If
    
            ShowEmployees()
    End Sub
  20. In the Class Name combo box, select btnNewEmployee
  21. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnNewEmployee_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewEmployee.Click
            Dim dlgEmpl As NewEmployee = New NewEmployee
    
            If dlgEmpl.ShowDialog() = DialogResult.OK Then
                If dlgEmpl.txtLastName.Text = "" Then
                    MsgBox("You must provide at least a last name to create a new employee")
                    Exit Sub
                Else
                    Dim strFilename As String = "Employees.bcr"
                    Dim empl As Employee = New Employee
    
                    empl.FirstName = dlgEmpl.txtFirstName.Text
                    empl.LastName = dlgEmpl.txtLastName.Text
                    empl.Title = dlgEmpl.txtTitle.Text
                    empl.Gender = dlgEmpl.cboGenders.Text
    
                    lstEmployees.Add(empl)
    
    Dim bcrStream As FileStream = New FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
                    Dim bcrSoap As SoapFormatter = New SoapFormatter
                    bcrSoap.Serialize(bcrStream, lstEmployees)
                    bcrStream.Close()
    
                    ShowEmployees()
                End If
            End If
    End Sub
  22. In the Class Name combo box, select btnClose
  23. In the Method Name combo box, select Click and implement the event as follows:
      
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  24. Display the Switchboard form
  25. Add a Button to the form and change its properties as follows:
    (Name): btnEmployees
    Text: Employees
  26. Double-click the Employees button to generate its Click event
  27. Implement the event as follows:
     
    Private Sub btnEmployees_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployees.Click
            Dim frmEmpl As Employees = New Employees
    
            frmEmpl.Show()
    End Sub
  28. Execute the application
  29. Create a few employees as follows:
     
    First Name Last Name Title Gender
    Patricia Katts Store Manager Female
    Henry Larson Sales Representative Male
    Gertrude Palau Sales Representative Female
    Helene Sandt Intern Female
    Melanie Karron Sales Representative Unknown
    Ernest Chisen Sales Manager Male
    Melissa Roberts Administrative Assistant Female
     
  30. Close the forms and return to your programming environment
 

Home Copyright © 2005-2016, FunctionX Next