Home

Collection-Based Application: Altair Realtors

     

Introduction

A real estate company or agency is a business that presents or sells houses (properties) to prospective customers. This is an application that addresses some of the issues with a real estate company.

   

ApplicationPractical Learning: Introducing the Collection Class

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Forms Application
  4. Change the Name to AltairRealtors1
  5. Click OK
  6. To create a new class, on the main menu, click Project -> Add Class...
  7. Set the Name to RealEstateProperty
  8. Click Add
  9. Change the file as follows:
    <Serializable()>
    Public Class RealEstateProperty
        Private nbr As String
        Private pType As String
        Private adrs As String
        Private ct As String
        Private stt As String
        Private zip As String
        Private levels As Byte
        Private yr As Integer
        Private beds As Byte
        Private baths As Single
        Private cond As String
        Private status As String
        Private cost As Double
        Private pFile As String
        Public Property PropertyNumber As String
    
            Get
                Return nbr
            End Get
            Set(ByVal value As String)
                nbr = value
            End Set
        End Property
    
        Public Property PropertyType As String
            Get
                Return pType
            End Get
            Set(ByVal value As String)
                pType = value
            End Set
        End Property
    
        Public Property Address As String
            Get
                Return adrs
            End Get
            Set(ByVal value As String)
                adrs = value
            End Set
        End Property
    
        Public Property City As String
            Get
                Return ct
            End Get
            Set(ByVal value As String)
                ct = value
            End Set
        End Property
    
        Public Property State As String
            Get
                Return stt
            End Get
            Set(ByVal value As String)
                stt = value
            End Set
        End Property
    
        Public Property ZIPCode As String
            Get
                Return zip
            End Get
            Set(ByVal value As String)
                zip = value
            End Set
        End Property
    
        Public Property Stories As Byte
            Get
                Stories = levels
            End Get
            Set(ByVal value As Byte)
                levels = value
            End Set
        End Property
    
        Public Property YearBuilt As Integer
            Get
                YearBuilt = yr
            End Get
            Set(ByVal value As Integer)
                yr = value
            End Set
        End Property
    
        Public Property Bedrooms As Byte
            Get
                Bedrooms = beds
            End Get
            Set(ByVal value As Byte)
                beds = value
            End Set
        End Property
    
        Public Property Bathrooms As Single
            Get
                Bathrooms = baths
            End Get
            Set(ByVal value As Single)
                baths = value
            End Set
        End Property
    
        Public Property Condition As String
            Get
                Condition = cond
            End Get
            Set(ByVal value As String)
                cond = value
            End Set
        End Property
    
        Public Property SaleStatus As String
            Get
                SaleStatus = status
            End Get
            Set(ByVal value As String)
                status = value
            End Set
        End Property
        Public Property MarketValue As Double
            Get
                MarketValue = cost
            End Get
            Set(ByVal value As Double)
                cost = value
            End Set
        End Property
    
        Public Property PictureFile As String
            Get
                PictureFile = pFile
            End Get
            Set(ByVal value As String)
                pFile = value
            End Set
        End Property
    
        ' To determine that two properties are the same,
        ' we will test only the property number.
        ' We assume that if two properties have the same number,
        ' then it is the same property
        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            Dim rep As RealEstateProperty = CType(obj, RealEstateProperty)
    
            If rep.PropertyNumber = PropertyNumber Then
                Equals = True
            Else
                Equals = False
            End If
        End Function
    End Class
  10. In the Solution Explorer, right-click Form1.vb and click Rename
  11. Type AltairRealtors.vb and press Enter twice to display the form
  12. From the Toolbox, add a list view to the form
  13. Right-click that list box on the click Edit Columns...
  14. Create the columns as follows:
     
    (Name) Text TextAlign Width
    colPropertyNumber Property #   65
    colCity City   75 
    colStories Stories Right 45
    colYearBuilt Year Right 40
    colBedrooms Beds Right 38
    colBathrooms Baths Right 40
    colCondition Condition   80
    colSaleStatus Status   70
    colMarketValue Value Right 75
  15. Click OK
  16. Design the form as follows:
     
    Altair Realtors - Properties Listing
    Control (Name) Anchor BorderStyle SizeMode  Text 
    ListView DataGridView lvwProperties Top, Bottom, Left, Right      
    Button btnNewProperty Bottom, Left     New Real Estate Property... 
    PictureBox pbxPicture Bottom, Right FixedSingle  Zoom   
    Button btnClose Bottom, Right      Close
    Form
    Text: Altair Realtors - Properties Listing
    StartPosition: CenterScreen
  17. Right-click the form and click Edit Groups...
  18. Create the groups as follows:
     
    Header Name
    Condominium lvgCondominium
    Townhouse lvgTownhouse
    Single Family lvgSingleFamily
  19. Click OK
  20. To create a new form, on the main menu, click Projects -> Add Windows Form...
  21. Set the Name to PropertyEditor
  22. Click Add
  23. In the Dialogs section of the Toolbox, click OpenFileDialog
  24. Click the form
  25. In the Properties window, change its characteristics as follows:
    (Name):      dlgPicture
    DefaultExt:  jpg
    Filter:         JPEG Files (*.jpg,*.jpeg)|*.jpg
    Title:          Select House Picture
  26. Design the form as follows:
     
    Altair Realtors - Property Editor
    Control (Name) DropDownStyle Text Items Modifiers Other Properties
    Label Label     Property Type:      
    ComboBox ComboBox cbxPropertyTypes DropDownList   Condominium
    Townhouse
    Single Family
    Unknown
    Public  
    Label Label     Property #:      
    TextBox TextBox txtPropertyNumber       Public  
    Label Label     Address:      
    TextBox TextBox txtAddress       Public  
    Label Label     City:      
    TextBox TextBox txtCity       Public  
    Label Label     State:      
    ComboBox ComboBox cbxStates DropDownList   AL, AK, AZ, AR, CA, CO, CT, DE, DC, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA, WV, WI, WY Public  
    Label Label     ZIP Code:      
    TextBox TextBox txtZIPCode       Public  
    Label Label     Stories:      
    TextBox TextBox txtStories       Public  
    Label Label     Year Built:      
    TextBox TextBox txtYearBuilt       Public  
    Label Label     Condition:      
    ComboBox ComboBox cbxConditions DropDownList    Excellent
    Good Shape
    Needs Fixing
    Public  
    Label Label     Bedrooms:      
    TextBox TextBox txtBedrooms   0   Public  
    Label Label     Bathrooms:      
    TextBox TextBox txtBathrooms   0.00   Public  
    Label Label     Market Value:      
    TextBox TextBox txtMarketValue   0.00   Public  
    Label Label     Sale Status:      
    ComboBox ComboBox cbxSaleStatus DropDownList   Unspecified
    Available
    Sold
    Public  
    Button Button btnPicture   Picture...      
    PictureBox PictureBox pbxProperty         BorderStyle: FixedSingle
    SizeMode: Zoom
    Button Button btnOK   OK     DialogResult: OK
    Button Button btnCancel   Cancel     DialogResult: Cancel
    Form
    FormBorderStyle: FixedDialog
    Text: Altair Realtors - Property Editor
    StartPosition: CenterScreen
    AcceptButton: btnOK
    CancelButton: btnCancel
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskBar: False
  27. Right-click the form and click View Code
  28. Declare two variables as follows:
    Public Class PropertyEditor
        Public PictureChanged As Boolean
        Public PictureFile As String
    End Class
  29. In the top left combo box, select (PropertyEditor Events)
  30. In the right combo box, selec Load
  31. Implement the event as follows:
    Public Class PropertyEditor
        Public PictureChanged As Boolean
        Public PictureFile As String
    
        Private Sub PropertyEditor_Load(ByVal sender As Object,
                                        ByVal e As System.EventArgs) Handles Me.Load
            PictureChanged = False
            PictureFile = "C:\Altair Realtors1\000-000.jpg"
        End Sub
    End Class
  32. In the top left combo box, select btnPicture
  33. In the right combo box, select Click
  34. Implement the event as follows:
    Public Class PropertyEditor
        Public PictureChanged As Boolean
        Public PictureFile As String
    
        Private Sub PropertyEditor_Load(ByVal sender As Object,
                                        ByVal e As System.EventArgs) Handles Me.Load
            PictureChanged = False
            PictureFile = "C:\Altair Realtors1\000-000.jpg"
        End Sub
    
        Private Sub btnPicture_Click(ByVal sender As Object,
                                     ByVal e As System.EventArgs) Handles btnPicture.Click
            If dlgPicture.ShowDialog() = DialogResult.OK Then
                pbxProperty.Image = Image.FromFile(dlgPicture.FileName)
                PictureFile = dlgPicture.FileName
                PictureChanged = True
            End If
        End Sub
    End Class

ApplicationPractical Learning: Starting a Linked List Class

  1. Click the AltairRealtors.vb [Design] tab
  2. Right-click the form and click View Code
  3. Change the file as follows:
    Imports System.IO
    Imports System.Collections.Generic
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Public Class AltairRealtors
        Private Properties As LinkedList(Of RealEstateProperty)
    
        Private Sub ShowProperties()
            ' Get a reference to the file that holds the records of properties
            Dim Filename As String = "C:\Altair Realtors\properties.atr"
    
            ' Make sure the file exists
            If File.Exists(Filename) = True Then
                ' if so, create a file stream
                Dim stmProperties As FileStream = New FileStream(Filename,
                                                          FileMode.Open,
                                                          FileAccess.Read)
                ' Create a binary formatter
                Dim bfmProperty As BinaryFormatter = New BinaryFormatter
                ' If some properties were created already,
                ' get them and store them in the collection
                Properties = CType(bfmProperty.Deserialize(stmProperties), LinkedList(Of RealEstateProperty))
    
                ' First, empty the list view
                lvwProperties.Items.Clear()
                Dim lviProperty As ListViewItem = Nothing
    
                ' Visit each property in the collection and add it to the list view
                For Each House As RealEstateProperty In Properties
                    If house.PropertyType.Equals("Condominium") Then
                        lviProperty = New ListViewItem(House.PropertyNumber, lvwProperties.Groups(0))
                    ElseIf House.PropertyType = "Townhouse" Then
                        lviProperty = New ListViewItem(House.PropertyNumber, lvwProperties.Groups(1))
                    Else ' If house.PropertyType.Equals("Single Family"))
                        lviProperty = New ListViewItem(House.PropertyNumber, lvwProperties.Groups(2))
    
                        lviProperty.SubItems.Add(house.City)
                        lviProperty.SubItems.Add(CStr(house.Stories))
                        lviProperty.SubItems.Add(CStr(house.YearBuilt))
                        lviProperty.SubItems.Add(CStr(house.Bedrooms))
                        lviProperty.SubItems.Add(FormatNumber(House.Bathrooms))
                        lviProperty.SubItems.Add(house.Condition)
                        lviProperty.SubItems.Add(house.SaleStatus)
                        lviProperty.SubItems.Add(CStr(House.MarketValue))
                        lvwProperties.Items.Add(lviProperty)
                    End If
                Next
    
                ' Close the file stream
                stmProperties.Close()
            End If
        End Sub
    
    End Class
  4. In the Names combo box, select AltairRealtors events
  5. In the Procedures combo box, select Load
  6. Implement the event as follows:
    Private Sub AltairRealtors_Load(ByVal sender As Object,
                                        ByVal e As System.EventArgs) Handles Me.Load
            Properties = New LinkedList(Of RealEstateProperty)
            ShowProperties()
    End Sub

ApplicationPractical Learning: Adding the First Node

  1. In the left combo box of the Code Editor, selec btnNewProperty
  2. In the right combo box, select Click
  3. Implement the event as follows:
    Private Sub btnNewProperty_Click(ByVal sender As Object,
                                         ByVal e As System.EventArgs) Handles btnNewProperty.Click
            Dim Editor As PropertyEditor = New PropertyEditor
    
            Dim RndNumber As Random = New Random(DateTime.Now.Millisecond)
            Dim Number1 As Integer = RndNumber.Next(100, 999)
            Dim Number2 As Integer = RndNumber.Next(100, 999)
            Dim propNumber As String = Number1 & "-" & Number2
    
            editor.txtPropertyNumber.Text = propNumber
    
            ' Check that the directory that contains the list of properties exists.
            ' If it doesn't exist, create it
            Dim DirInfo As DirectoryInfo = Directory.CreateDirectory("C:\\Altair Realtors1")
            ' Get a reference to the file that holds the properties
            Dim Filename As String = "C:\Altair Realtors1\properties.atr"
    
            ' First check if the file was previously created
            If File.Exists(Filename) = True Then
                ' If the list of properties exists already,
                ' get it and store it in a file stream
                Dim stmProperties As FileStream = New FileStream(Filename,
                                                          FileMode.Open,
                                                          FileAccess.Read)
                Dim bfmProperty As BinaryFormatter = New BinaryFormatter
                ' Store the list of properties in the collection
                Properties = CType(bfmProperty.Deserialize(stmProperties), LinkedList(Of RealEstateProperty))
                ' Close the file stream
                stmProperties.Close()
            End If
    
            If editor.ShowDialog() = DialogResult.OK Then
                Dim Prop As RealEstateProperty = New RealEstateProperty
    
                Prop.PropertyNumber = Editor.txtPropertyNumber.Text
                Prop.PropertyType = Editor.cbxPropertyTypes.Text
                Prop.Address = Editor.txtAddress.Text
                Prop.City = Editor.txtCity.Text
                Prop.State = Editor.cbxStates.Text
                Prop.ZIPCode = Editor.txtZIPCode.Text
                Prop.Stories = CByte(Editor.txtStories.Text)
                Prop.YearBuilt = CInt(Editor.txtYearBuilt.Text)
                Prop.Bedrooms = CByte(Editor.txtBedrooms.Text)
                Prop.Bathrooms = CSng(Editor.txtBathrooms.Text)
                Prop.Condition = Editor.cbxConditions.Text
                Prop.SaleStatus = Editor.cbxSaleStatus.Text
                Prop.MarketValue = CDbl(Editor.txtMarketValue.Text)
                If Editor.PictureFile <> "" Then
                    Dim PictureFile As FileInfo = New FileInfo(Editor.PictureFile)
                    PictureFile.CopyTo("C:\Altair Realtors1\" &
                                  Editor.txtPropertyNumber.Text &
                                  PictureFile.Extension)
                    Prop.PictureFile = "C:\Altair Realtors1\" &
                                      Editor.txtPropertyNumber.Text &
                                      PictureFile.Extension
                Else
                    Prop.PictureFile = "C:\Altair Realtors1\000-000.jpg"
                End If
    
                ' Add the property in the collection
                Properties.AddFirst(Prop)
    
                ' Get a reference to the properties file
                Dim strFilename As String = DirInfo.FullName & "\properties.atr"
                ' Create a file stream to hold the list of properties
                Dim stmProperties As FileStream = New FileStream(strFilename,
                                                          FileMode.Create,
                                                          FileAccess.Write)
                Dim bfmProperty As BinaryFormatter = New BinaryFormatter
    
                ' Serialize the list of properties
                bfmProperty.Serialize(stmProperties, Properties)
                ' Close the file stream
                stmProperties.Close()
    
                ' Show the list of properties
                ShowProperties()
    End If
        End Sub
  4. In the left combo box of the Code Editor, select lvwProperties
  5. In the Events (right) combo box, select ItemSelectionChanged
  6. Implement the event as follows:
    Private Sub lvwProperties_ItemSelectionChanged(ByVal sender As Object,
                                                       ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvwProperties.ItemSelectionChanged
            Dim CurrentProperty As RealEstateProperty = New RealEstateProperty
    
            For Each Prop As RealEstateProperty In Properties
                If Prop.PropertyNumber = e.Item.SubItems(0).Text Then
                    pbxProperty.Image = Image.FromFile(Prop.PictureFile)
                End If
            Next
    End Sub
  7. To execute, press F5
  8. Click the New Real Estate Property button and create a property
     
    Altair Realtors
  9. Click OK
  10. Copy the following picture and paste (or save it) in the Altair Realtors folder on the C: drive (or the folder that contains the file properties of this project):
     
    Default property
  11. Create some properties
     
    Altair Realtors
  12. Close the form and return to your programming environment
 
 
 

ApplicationPractical Learning: Checking a Node

  1. In the Names combo box, select lvwProperties
  2. In the Procedures combo box, select DoubleClick
  3. Implement the event as follows:
    Private Sub lvwProperties_DoubleClick(ByVal sender As Object,
                  ByVal e As System.EventArgs) Handles lvwProperties.DoubleClick
            If (lvwProperties.SelectedItems.Count = 0) Or
               (lvwProperties.SelectedItems.Count > 1) Then
                Exit Sub
            End If
    
            ' Get a reference to the file that holds the properties
            Dim Filename As String = "C:\Altair Realtors\properties.atr"
    
            ' Open the file that contains the properties
            Dim stmProperties As FileStream = New FileStream(Filename,
                                                      FileMode.Open,
                                                      FileAccess.Read)
            Dim bfmProperty As BinaryFormatter = New BinaryFormatter
            ' Store the list of properties in the collection
            Properties = bfmProperty.Deserialize(stmProperties)
            ' Close the file stream
            stmProperties.Close()
    
            ' Get the property that the user double-clicked
            Dim lviProperty As ListViewItem = lvwProperties.SelectedItems(0)
            Dim House As RealEstateProperty = New RealEstateProperty
            For Each Prop As RealEstateProperty In Properties
                If Prop.PropertyNumber = lviProperty.Text Then
                    House = Prop
                End If
            Next
    
            ' Get a reference to the property editor
            Dim Editor As PropertyEditor = New PropertyEditor
    
            ' Prepare to fill the editor with the
            ' values of the property the user double-clicked
            Editor.txtPropertyNumber.Text = House.PropertyNumber
            Editor.cbxPropertyTypes.Text = House.PropertyType
            Editor.txtAddress.Text = House.Address
            Editor.txtCity.Text = House.City
            Editor.cbxStates.Text = House.State
            Editor.txtZIPCode.Text = House.ZIPCode
            editor.txtStories.Text = CStr(house.Stories)
            editor.txtYearBuilt.Text = CStr(house.YearBuilt)
            editor.txtBedrooms.Text = CStr(house.Bedrooms)
            editor.txtBathrooms.Text = FormatNumber(house.Bathrooms)
            editor.cbxConditions.Text = house.Condition
            editor.cbxSaleStatus.Text = house.SaleStatus
            editor.txtMarketValue.Text = FormatNumber(house.MarketValue)
            editor.pbxProperty.Image = Image.FromFile(house.PictureFile)
    
            ' Disable the property number just in
            ' case the user tries to change it
            Editor.txtPropertyNumber.Enabled = False
    
            ' Show the property editor
            editor.ShowDialog()
    End Sub
  4. Press F5 to execute the application
  5. Double-click one of the properties
  6. Close the forms and return to your programming environment

ApplicationPractical Learning: Updating a Node

  1. Change the DoubleClick event as follows:
    Private Sub lvwProperties_DoubleClick(ByVal sender As Object,
                   ByVal e As System.EventArgs) Handles lvwProperties.DoubleClick
            If (lvwProperties.SelectedItems.Count = 0) Or
               (lvwProperties.SelectedItems.Count > 1) Then
                Exit Sub
            End If
    
            ' Get a reference to the file that holds the properties
            Dim Filename As String = "C:\Altair Realtors\properties.atr"
    
            ' Open the file that contains the properties
            Dim stmProperties As FileStream = New FileStream(Filename,
                                                      FileMode.Open,
                                                      FileAccess.Read)
            Dim bfmProperty As BinaryFormatter = New BinaryFormatter
            ' Store the list of properties in the linked list
            Properties = bfmProperty.Deserialize(stmProperties)
            ' Close the file stream
            stmProperties.Close()
    
            ' Get the property that the user double-clicked
            Dim lviProperty = lvwProperties.SelectedItems(0)
            Dim House As RealEstateProperty = New RealEstateProperty
            ' Get the property number the user double-clicked
            House.PropertyNumber = lviProperty.Text
    
            ' Find the property in the list
            Dim NodeProperty As LinkedListNode(Of RealEstateProperty) = Properties.Find(House)
    
            ' Get a reference to the property editor
            Dim Editor As PropertyEditor = New PropertyEditor
    
            ' Prepare to fill the editor with the values of the property the user double-clicked
            Editor.txtPropertyNumber.Text = NodeProperty.Value.PropertyNumber
            Editor.cbxPropertyTypes.Text = NodeProperty.Value.PropertyType
            Editor.txtAddress.Text = NodeProperty.Value.Address
            Editor.txtCity.Text = NodeProperty.Value.City
            Editor.cbxStates.Text = NodeProperty.Value.State
            Editor.txtZIPCode.Text = NodeProperty.Value.ZIPCode
            Editor.txtStories.Text = CStr(NodeProperty.Value.Stories)
            Editor.txtYearBuilt.Text = CStr(NodeProperty.Value.YearBuilt)
            Editor.txtBedrooms.Text = CStr(NodeProperty.Value.Bedrooms)
            Editor.txtBathrooms.Text = FormatNumber(NodeProperty.Value.Bathrooms)
            Editor.cbxConditions.Text = NodeProperty.Value.Condition
            Editor.cbxSaleStatus.Text = NodeProperty.Value.SaleStatus
            Editor.txtMarketValue.Text = FormatNumber(NodeProperty.Value.MarketValue)
            Editor.pbxProperty.Image = Image.FromFile(NodeProperty.Value.PictureFile)
            Editor.PictureFile = NodeProperty.Value.PictureFile
    
            ' Disable the property number so the user cannot change it
            Editor.txtPropertyNumber.Enabled = False
    
            ' Show the property editor
            If Editor.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                ' For each value that has changed in the property, update its node
                NodeProperty.Value.PropertyType = Editor.cbxPropertyTypes.Text
                NodeProperty.Value.Address = Editor.txtAddress.Text
                NodeProperty.Value.City = Editor.txtCity.Text
                NodeProperty.Value.State = Editor.cbxStates.Text
                NodeProperty.Value.ZIPCode = Editor.txtZIPCode.Text
                NodeProperty.Value.Stories = CByte(Editor.txtStories.Text)
                NodeProperty.Value.YearBuilt = CInt(Editor.txtYearBuilt.Text)
                NodeProperty.Value.Bedrooms = CByte(Editor.txtBedrooms.Text)
                NodeProperty.Value.Bathrooms = CSng(Editor.txtBathrooms.Text)
                NodeProperty.Value.Condition = Editor.cbxConditions.Text
                NodeProperty.Value.SaleStatus = Editor.cbxSaleStatus.Text
                NodeProperty.Value.MarketValue = CDbl(Editor.txtMarketValue.Text)
                NodeProperty.Value.PictureFile = Editor.PictureFile
    
                ' Start saving the linked list
                stmProperties = New FileStream(Filename,
                                               FileMode.OpenOrCreate,
                                               FileAccess.ReadWrite)
                bfmProperty = New BinaryFormatter
    
                ' Serialize the list of properties
                bfmProperty.Serialize(stmProperties, Properties)
                ' Close the file stream
                stmProperties.Close()
    
                ' Show the list of properties
                ShowProperties()
            End If
        End Sub
  2. Press F5 to execute
  3. Double-click the row in the Townhouse section
  4. Change its year built to 2005
  5. Change its market value to 585985 (If possible, also change its picture)
     
    Altair Realtors
  6. Click OK
     
    Altair Realtors
  7. Close the form and return to your programming environment

ApplicationPractical Learning: Navigating Among Nodes

  1. To create a new form, on the main menu, click Projects -> Add Windows Form...
  2. Set the Name to PropertiesReview
  3. Click Add
  4. Design the form as follows:
     
    Altair Realtors
    Control (Name) DropDownStyle Text Other Properties
    Label Label     Property #:  
    TextBox TextBox txtPropertyNumber      
    Label Label     Property Type:  
    TextBox TextBox txtPropertyType      
    Label Label     Address:  
    TextBox TextBox txtAddress      
    Label Label     City:  
    TextBox TextBox txtCity      
    Label Label     State:  
    TexBox TextBox txtState      
    Label Label     ZIP Code:  
    TextBox TextBox txtZIPCode      
    Label Label     Stories:  
    TextBox TextBox txtStories      
    Label Label     Year Built:  
    TextBox TextBox txtYearBuilt      
    Label Label     Condition:  
    TextBox TextBox txtCondition      
    Label Label     Bedrooms:  
    TextBox TextBox txtBedrooms   0  
    Label Label     Bathrooms:  
    TextBox TextBox txtBathrooms   0.00  
    Label Label     Market Value:  
    TextBox TextBox txtMarketValue   0.00  
    Label Label     Sale Status:  
    TextBox TextBox txtStatus      
    PictureBox PictureBox pbxProperty     BorderStyle: FixedSingle
    SizeMode: Zoom
    Button Button btnClose   Close  
    Button Button btnFirst   First  
    Label Label lblRecordNumber   000 of 000  
    Button Button btnPrevious   Previous  
    Button Button btnLast   Last  
  5. Right-click the form and click View Code
  6. Change the file as follows:
    Imports System.IO
    Imports System.Collections.Generic
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Public Class PropertiesReview
        Private Index As Integer
        Private Properties As LinkedList(Of RealEstateProperty)
        Dim CurrentNode As LinkedListNode(Of RealEstateProperty)
    End Class
  7. In the Names combo box, select (PropertiesReview Events)
  8. In the Procedures combo box, select Load
  9. Implement the event as follows:
    Imports System.IO
    Imports System.Collections.Generic
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Public Class PropertiesReview
        Private Index As Integer
        Private Properties As LinkedList(Of RealEstateProperty)
        Dim CurrentNode As LinkedListNode(Of RealEstateProperty)
    
        Private Sub PropertiesReview_Load(ByVal sender As Object,
                                          ByVal e As System.EventArgs) Handles Me.Load
            Index = 1
            ' Get a reference to the file that holds the records of properties
            Dim Filename As String = "C:\Altair Realtors\properties.atr"
    
            ' Make sure the file exists
            If File.Exists(Filename) = True Then
                ' if so, create a file stream
                Dim stmProperties As FileStream = New FileStream(Filename,
                                                          FileMode.Open,
                                                          FileAccess.Read)
                ' Create a binary formatter
                Dim bfmProperty As BinaryFormatter = New BinaryFormatter
                ' If some properties were created already,
                ' get them and store them in the collection
                Properties = bfmProperty.Deserialize(stmProperties)
            End If
        End Sub
    End Class
  10. In the Names combo box, select btnFirst
  11. In the Procedures combo box, select Click
  12. Implement the event as follows:
    Imports System.IO
    Imports System.Collections.Generic
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Public Class PropertiesReview
        Private Index As Integer
        Private Properties As LinkedList(Of RealEstateProperty)
        Dim CurrentNode As LinkedListNode(Of RealEstateProperty)
    
        Private Sub PropertiesReview_Load(ByVal sender As Object,
                                          ByVal e As System.EventArgs) Handles Me.Load
            Index = 1
            ' Get a reference to the file that holds the records of properties
            Dim Filename As String = "C:\Altair Realtors\properties.atr"
    
            ' Make sure the file exists
            If File.Exists(Filename) = True Then
                ' if so, create a file stream
                Dim stmProperties As FileStream = New FileStream(Filename,
                                                          FileMode.Open,
                                                          FileAccess.Read)
                ' Create a binary formatter
                Dim bfmProperty As BinaryFormatter = New BinaryFormatter
                ' If some properties were created already,
                ' get them and store them in the collection
                Properties = bfmProperty.Deserialize(stmProperties)
            End If
    
            btnFirst_Click(sender, e)
        End Sub
    
        Private Sub btnFirst_Click(ByVal sender As Object,
                                   ByVal e As System.EventArgs) Handles btnFirst.Click
            Index = 1
            CurrentNode = Properties.First
    
            txtPropertyNumber.Text = CurrentNode.Value.PropertyNumber
            txtPropertyType.Text = CurrentNode.Value.PropertyType
            txtAddress.Text = CurrentNode.Value.Address
            txtCity.Text = CurrentNode.Value.City
            txtState.Text = CurrentNode.Value.State
            txtZIPCode.Text = CurrentNode.Value.ZIPCode
            txtStories.Text = CStr(CurrentNode.Value.Stories)
            txtYearBuilt.Text = CStr(CurrentNode.Value.YearBuilt)
            txtBedrooms.Text = CStr(CurrentNode.Value.Bedrooms)
            txtBathrooms.Text = FormatNumber(CurrentNode.Value.Bathrooms)
            txtCondition.Text = CurrentNode.Value.Condition
            txtSaleStatus.Text = CurrentNode.Value.SaleStatus
            txtMarketValue.Text = FormatNumber(CurrentNode.Value.MarketValue)
            pbxProperty.Image = Image.FromFile(CurrentNode.Value.PictureFile)
    
            lblRecordNumber.Text = "1 of " & CStr(Properties.Count)
        End Sub
    End Class
  13. In the Names combo box, select btnPrevious
  14. In the Procedures combo box, select Click
  15. Implement the event as follows:
    Private Sub btnPrevious_Click(ByVal sender As Object,
                       ByVal e As System.EventArgs) Handles btnPrevious.Click
            Index = Index - 1
    
            If Index <= 1 Then
                btnFirst_Click(sender, e)
            Else
                Dim Current As LinkedListNode(Of RealEstateProperty) = CurrentNode.Previous
    
                txtPropertyNumber.Text = Current.Value.PropertyNumber
                txtPropertyType.Text = Current.Value.PropertyType
                txtAddress.Text = Current.Value.Address
                txtCity.Text = Current.Value.City
                txtState.Text = Current.Value.State
                txtZIPCode.Text = Current.Value.ZIPCode
                txtStories.Text = CStr(Current.Value.Stories)
                txtYearBuilt.Text = CStr(Current.Value.YearBuilt)
                txtBedrooms.Text = CStr(Current.Value.Bedrooms)
                txtBathrooms.Text = FormatNumber(Current.Value.Bathrooms)
                txtCondition.Text = Current.Value.Condition
                txtSaleStatus.Text = Current.Value.SaleStatus
                txtMarketValue.Text = FormatNumber(Current.Value.MarketValue)
    
                lblRecordNumber.Text = Index & " of " & CStr(Properties.Count)
            End If
    End Sub
  16. In the Names combo box, select btnNext
  17. In the Procedures combo box, select Click
  18. In the Names combo box, select btnLast
  19. In the Procedures combo box, select Click
  20. Implement the events as follows:
    Private Sub btnNext_Click(ByVal sender As Object,
                              ByVal e As System.EventArgs) Handles btnNext.Click
            Index = Index + 1
    
            If Index >= Properties.Count Then
                btnLast_Click(sender, e)
            Else
                Dim Current As LinkedListNode(Of RealEstateProperty) = CurrentNode.Next
    
                txtPropertyNumber.Text = Current.Value.PropertyNumber
                txtPropertyType.Text = Current.Value.PropertyType
                txtAddress.Text = Current.Value.Address
                txtCity.Text = Current.Value.City
                txtState.Text = Current.Value.State
                txtZIPCode.Text = Current.Value.ZIPCode
                txtStories.Text = CStr(Current.Value.Stories)
                txtYearBuilt.Text = CStr(Current.Value.YearBuilt)
                txtBedrooms.Text = CStr(Current.Value.Bedrooms)
                txtBathrooms.Text = FormatNumber(Current.Value.Bathrooms)
                txtCondition.Text = Current.Value.Condition
                txtSaleStatus.Text = Current.Value.SaleStatus
                txtMarketValue.Text = FormatNumber(Current.Value.MarketValue)
                pbxProperty.Image = Image.FromFile(Current.Value.PictureFile)
    
                lblRecordNumber.Text = CStr(Index) & " of " & CStr(Properties.Count)
            End If
    End Sub
    
    Private Sub btnLast_Click(ByVal sender As Object,
                              ByVal e As System.EventArgs) Handles btnLast.Click
            Index = Properties.Count
            CurrentNode = Properties.Last
    
            txtPropertyNumber.Text = CurrentNode.Value.PropertyNumber
            txtPropertyType.Text = CurrentNode.Value.PropertyType
            txtAddress.Text = CurrentNode.Value.Address
            txtCity.Text = CurrentNode.Value.City
            txtState.Text = CurrentNode.Value.State
            txtZIPCode.Text = CurrentNode.Value.ZIPCode
            txtStories.Text = CStr(CurrentNode.Value.Stories)
            txtYearBuilt.Text = CStr(CurrentNode.Value.YearBuilt)
            txtBedrooms.Text = CStr(CurrentNode.Value.Bedrooms)
            txtBathrooms.Text = FormatNumber(CurrentNode.Value.Bathrooms)
            txtCondition.Text = CurrentNode.Value.Condition
            txtSaleStatus.Text = CurrentNode.Value.SaleStatus
            txtMarketValue.Text = FormatNumber(CurrentNode.Value.MarketValue)
            pbxProperty.Image = Image.FromFile(CurrentNode.Value.PictureFile)
    
            lblRecordNumber.Text = CStr(Properties.Count) & " of " & CStr(Properties.Count)
    End Sub
  21. In the Names combo box, select btnClose
  22. In the Procedures combo box, select Click
  23. 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 Properties Listing form
  25. On the right side of the New Real Estate Property button, add a new button and set its characteristics as follows:
    (Name): btnReviewProperties
    Text: Review Properties...
  26. Double-click the Navigate button and implement its event as follows:
    Private Sub btnReviewProperties_Click(ByVal sender As Object,
                     ByVal e As System.EventArgs) Handles btnReviewProperties.Click
            Dim pr As PropertiesReview = New PropertiesReview
    
            pr.ShowDialog()
    End Sub
  27. Press F5 to execute
  28. Click the Review Properties button
  29. Close the forms and return to your programming environment

ApplicationPractical Learning: Inserting Nodes

  1. Display the Properties Listing form
  2. In the Menus & Toolbars section of the Toolbox, click ContextMenuStrip and click the form
  3. Click the menu items as follows:
     
    (Name) Enabled Text
    mnuNewProperty   New Property...
    mnuEditProperty False Edit Property...
    mnuInsertBefore False Insert Property...
    mnuInsertAfter False Insert After
  4. Under the form, click the context menu strip
  5. In the Properties window, change its name to cmsProperties
  6. On the form, click the list view
  7. In the Properties window, set its ContextMenuStrip to cmsProperties
  8. Right-click the form and click View Code
  9. In the Names combo box, select mnuNewProperty
  10. In the Procedures combo box, select Click
  11. Implement the event as follows:
    Private Sub mnuNewProperty_Click(ByVal sender As Object,
                        ByVal e As System.EventArgs) Handles mnuNewProperty.Click
            btnNewProperty_Click(sender, e)
    End Sub
  12. Locate the ItemSelectionChanged event and change its implementation as follows:
    Private Sub lvwProperties_ItemSelectionChanged(ByVal sender As Object,
                                                   ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvwProperties.ItemSelectionChanged
            Dim CurrentProperty As RealEstateProperty = New RealEstateProperty
    
            For Each Prop As RealEstateProperty In Properties
                If Prop.PropertyNumber = e.Item.SubItems(0).Text Then
                    pbxProperty.Image = Image.FromFile(Prop.PictureFile)
                End If
            Next
    
            If lvwProperties.SelectedItems.Count = 1 Then
                mnuInsertBefore.Enabled = True
                mnuEditProperty.Enabled = True
                mnuInsertAfter.Enabled = True
            Else
                mnuInsertBefore.Enabled = False
                mnuEditProperty.Enabled = False
                mnuInsertAfter.Enabled = False
            End If
    End Sub
  13. In the Names combo box, select mnuEditProperties
  14. In the Procedures combo box, select Click
  15. Implement the event as follows:
    Private Sub mnuEditProperty_Click(ByVal sender As Object,
                                      ByVal e As System.EventArgs) Handles mnuEditProperty.Click
        lvwProperties_DoubleClick(sender, e)
    End Sub
  16. In the Names combo box, select mnuInsertBefore
  17. In the Procedures combo box, select Click
  18. Implement the event as follows:
    Private Sub mnuInsertBefore_Click(ByVal sender As Object,
                                          ByVal e As System.EventArgs) Handles mnuInsertBefore.Click
            Dim Editor As PropertyEditor = New PropertyEditor
            Dim Filename As String = "C:\Altair Realtors1\properties.atr"
            Dim DirInfo As DirectoryInfo = Directory.CreateDirectory("C:\\Altair Realtors1")
    
            If File.Exists(Filename) = True Then
                Dim stmProperties As FileStream = New FileStream(Filename,
                                                          FileMode.Open,
                                                          FileAccess.Read)
                Dim bfmProperties As BinaryFormatter = New BinaryFormatter
    
                Properties = bfmProperties.Deserialize(stmProperties)
                stmProperties.Close()
                stmProperties.Close()
            End If
    
            Dim rep As RealEstateProperty = New RealEstateProperty
            Dim strPropertyNumber As String = lvwProperties.SelectedItems(0).Text
            rep.PropertyNumber = lvwProperties.SelectedItems(0).Text
    
            Dim RndNumber As Random = New Random(DateTime.Now.Millisecond)
            Dim Number1 As Integer = RndNumber.Next(100, 999)
            Dim Number2 As Integer = RndNumber.Next(100, 999)
            Dim PropNumber As String = Number1 + "-" & Number2
    
            Editor.txtPropertyNumber.Text = PropNumber
            Editor.cbxPropertyTypes.Text = lvwProperties.SelectedItems(0).Group.Header
            Editor.cbxPropertyTypes.Enabled = False
    
            Dim NodeProperty As LinkedListNode(Of RealEstateProperty) = Properties.Find(rep)
    
            If NodeProperty Is Nothing Then
                If editor.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                    Dim Prop As RealEstateProperty = New RealEstateProperty
    
                    Prop.PropertyNumber = Editor.txtPropertyNumber.Text
                    Prop.PropertyType = Editor.cbxPropertyTypes.Text
                    Prop.Address = Editor.txtAddress.Text
                    Prop.City = Editor.txtCity.Text
                    Prop.State = Editor.cbxStates.Text
                    Prop.ZIPCode = Editor.txtZIPCode.Text
                    Prop.Stories = CByte(Editor.txtStories.Text)
                    Prop.YearBuilt = CInt(Editor.txtYearBuilt.Text)
                    Prop.Bedrooms = CByte(Editor.txtBedrooms.Text)
                    Prop.Bathrooms = CSng(Editor.txtBathrooms.Text)
                    Prop.Condition = Editor.cbxConditions.Text
                    Prop.SaleStatus = Editor.cbxSaleStatus.Text
                    Prop.MarketValue = CDbl(Editor.txtMarketValue.Text)
                    If Not Editor.PictureFile = "" Then
                        Dim PictureFile As FileInfo = New FileInfo(Editor.PictureFile)
                        PictureFile.CopyTo("C:\Altair Realtors1\" &
                                          Editor.txtPropertyNumber.Text &
                                          PictureFile.Extension)
                        Prop.PictureFile = "C:\Altair Realtors1\" &
                                          Editor.txtPropertyNumber.Text &
                                          PictureFile.Extension
                    Else
                        Prop.PictureFile = "C:\Altair Realtors1\000-000.jpg"
                    End If
    
                    ' Insert the property before the currently selected node
                    Properties.AddBefore(NodeProperty, Prop)
    
                    ' Get a reference to the properties file
                    Dim strFilename As String = DirInfo.FullName & "\properties.atr"
                    ' Create a file stream to hold the list of properties
                    Dim stmProperties As FileStream = New FileStream(strFilename,
                                                              FileMode.Create,
                                                              FileAccess.Write)
                    Dim bfmProperty As BinaryFormatter = New BinaryFormatter
    
                    ' Serialize the list of properties
                    bfmProperty.Serialize(stmProperties, Properties)
                    ' Close the file stream
                    stmProperties.Close()
    
                    ' Show the list of properties
                    ShowProperties()
                End If
            End If
    End Sub
  19. In the Names combo box, select mnuInsertAfter
  20. In the Procedures combo box, select Click
  21. Implement the event as follows:
    Private Sub mnuInsertAfter_Click(ByVal sender As Object,
                                         ByVal e As System.EventArgs) Handles mnuInsertAfter.Click
            Dim Editor As PropertyEditor = New PropertyEditor
            Dim Filename As String = "C:\Altair Realtors1\properties.atr"
            Dim DirInfo As DirectoryInfo = Directory.CreateDirectory("C:\\Altair Realtors1")
    
            If File.Exists(Filename) = True Then
                Dim stmProperties As FileStream = New FileStream(Filename,
                                                          FileMode.Open,
                                                          FileAccess.Read)
                Dim bfmProperties As BinaryFormatter = New BinaryFormatter
    
                Properties = bfmProperties.Deserialize(stmProperties)
                ' Close the file stream
                stmProperties.Close()
                stmProperties.Close()
            End If
    
            Dim rep As RealEstateProperty = New RealEstateProperty
            Dim strPropertyNumber As String = lvwProperties.SelectedItems(0).Text
            rep.PropertyNumber = lvwProperties.SelectedItems(0).Text
    
            Dim RndNumber As Random = New Random(DateTime.Now.Millisecond)
            Dim Number1 As Integer = RndNumber.Next(100, 999)
            Dim Number2 As Integer = RndNumber.Next(100, 999)
            Dim PropNumber As String = Number1 & "-" & Number2
    
            Editor.txtPropertyNumber.Text = PropNumber
            Editor.cbxPropertyTypes.Text = lvwProperties.SelectedItems(0).Group.Header
            Editor.cbxPropertyTypes.Enabled = False
    
            Dim NodeProperty As LinkedListNode(Of RealEstateProperty) = Properties.Find(rep)
    
            If NodeProperty Is Nothing Then
                If Editor.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                    Dim Prop As RealEstateProperty = New RealEstateProperty
    
                    Prop.PropertyNumber = Editor.txtPropertyNumber.Text
                    Prop.PropertyType = Editor.cbxPropertyTypes.Text
                    Prop.Address = Editor.txtAddress.Text
                    Prop.City = Editor.txtCity.Text
                    Prop.State = Editor.cbxStates.Text
                    Prop.ZIPCode = Editor.txtZIPCode.Text
                    Prop.Stories = CByte(Editor.txtStories.Text)
                    Prop.YearBuilt = CInt(Editor.txtYearBuilt.Text)
                    Prop.Bedrooms = CByte(Editor.txtBedrooms.Text)
                    Prop.Bathrooms = CSng(Editor.txtBathrooms.Text)
                    Prop.Condition = Editor.cbxConditions.Text
                    Prop.SaleStatus = Editor.cbxSaleStatus.Text
                    Prop.MarketValue = CDbl(Editor.txtMarketValue.Text)
                    If Not Editor.PictureFile = "" Then
                        Dim PictureFile As FileInfo = New FileInfo(Editor.PictureFile)
                        PictureFile.CopyTo("C:\Altair Realtors1\" &
                                          Editor.txtPropertyNumber.Text &
                                          PictureFile.Extension)
                        Prop.PictureFile = "C:\Altair Realtors1\" &
                                      Editor.txtPropertyNumber.Text &
                                      PictureFile.Extension
                    Else
                        Prop.PictureFile = "C:\Altair Realtors1\000-000.jpg"
                    End If
    
                    ' Insert the property before the currently selected node
                    Properties.AddAfter(NodeProperty, Prop)
    
                    ' Get a reference to the properties file
                    Dim strFilename As String = DirInfo.FullName & "\properties.atr"
                    ' Create a file stream to hold the list of properties
                    Dim stmProperties As FileStream = New FileStream(strFilename,
                                                              FileMode.Create,
                                                              FileAccess.Write)
                    Dim bfmProperty As BinaryFormatter = New BinaryFormatter
    
                    ' Serialize the list of properties
                    bfmProperty.Serialize(stmProperties, Properties)
                    ' Close the file stream
                    stmProperties.Close()
    
                    ' Show the list of properties
                    ShowProperties()
                End If
            End If
    End Sub
  22. To execute, press F5
  23. Create some properties (let the application generate the property number) (the new records are in bold):
  24. Close the form and return to your programming environment

ApplicationPractical Learning: Deleting a Node

  1. Display the Properties Listing form
  2. Under the form, click cmsProperties
  3. Add a new menu item as follows:
     
    (Name) Enabled Text
    mnuNewProperty   New Property...
    mnuEditProperty False Edit Property...
    mnuInsertBefore False Insert Property...
    mnuInsertAfter False Insert After
    mnuDeleteProperty False Delete Property
  4. Right-click the form and click View Code
  5. In the file, locate the ItemSelectionChanged event and change its implementation as follows:
    Private Sub lvwProperties_ItemSelectionChanged(ByVal sender As Object,
              ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvwProperties.ItemSelectionChanged
            Dim CurrentProperty As RealEstateProperty = New RealEstateProperty
    
            For Each Prop As RealEstateProperty In Properties
                If Prop.PropertyNumber = e.Item.SubItems(0).Text Then
                    pbxProperty.Image = Image.FromFile(Prop.PictureFile)
                End If
            Next
    
            If lvwProperties.SelectedItems.Count = 1 Then
                mnuInsertBefore.Enabled = True
                mnuEditProperty.Enabled = True
                mnuInsertAfter.Enabled = True
                mnuDeleteProperty.Enabled = True
            Else
                mnuInsertBefore.Enabled = False
                mnuEditProperty.Enabled = False
                mnuInsertAfter.Enabled = False
                mnuDeleteProperty.Enabled = False
            End If
        End Sub
  6. In the Names combo box, select mnuDeleteProperty
  7. In the Procedures combo box, select Click
  8. Implement the event as follows:
    Private Sub mnuDeleteProperty_Click(ByVal sender As Object,
                     ByVal e As System.EventArgs) Handles mnuDeleteProperty.Click
            Dim Editor As PropertyEditor = New PropertyEditor
            Dim Filename As String = "C:\Altair Realtors\properties.atr"
            Dim DirInfo As DirectoryInfo = 
            	Directory.CreateDirectory("C:\Altair Realtors")
    
            ' Open the file that holds the properties
            If File.Exists(Filename) = True Then
                Dim stmProperties As FileStream = New FileStream(Filename,
                                                          FileMode.Open,
                                                          FileAccess.Read)
                Dim bfmProperties As BinaryFormatter = New BinaryFormatter
    
                Properties = bfmProperties.Deserialize(stmProperties)
                stmProperties.Close()
                stmProperties.Close()
            End If
    
            ' Create a real estate property using the property number that the user double-clicked
            Dim rep As RealEstateProperty = New RealEstateProperty
            Dim strPropertyNumber As String = lvwProperties.SelectedItems(0).Text
            rep.PropertyNumber = lvwProperties.SelectedItems(0).Text
    
            ' Ask the compiler to locate that property
            Dim NodeProperty As LinkedListNode(Of RealEstateProperty) = 
            						Properties.Find(rep)
    
            ' Just in case, make sure the property was found
            If Not NodeProperty Is Nothing Then
                ' Present a warning message to the user
                If MsgBox("Are you sure you want to delete this property?",
                          MsgBoxStyle.YesNo,
                                    "Altair Realtors") = MsgBoxResult.Yes Then
                    ' If the user clicks yes, delete the property
                    Properties.Remove(NodeProperty)
    
                    ' Save the list of properties
                    Dim strFilename As String = DirInfo.FullName + "\properties.atr"
                    Dim stmProperties As FileStream = New FileStream(strFilename,
                                                              FileMode.Create,
                                                              FileAccess.Write)
                    Dim bfmProperty As BinaryFormatter = New BinaryFormatter
    
                    bfmProperty.Serialize(stmProperties, Properties)
                    stmProperties.Close()
    
                    ' Show the new list of properties
                    ShowProperties()
                End If
            End If
    End Sub
  9. In the Names combo box, select btnClose
  10. In the Procedures combo box, select Click
  11. Implement the event as follows:
    Private Sub btnClose_Click(ByVal sender As Object,
                           ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  12. To execute, press F5
  13. Right-click one of the records and click Delete Property
  14. Click No
  15. Right-click another row and click Delete Property
  16. Click Yes
  17. Close the form and return to your programming environment
 
 
   
 

Home Copyright © 2010-2016, FunctionX