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. In the middle list, click C++ Class and click Add
  8. Set the Name to CRealEstateProperty
  9. Click Finish
  10. Change the header file as follows:
    #pragma once
    
    using namespace System;
    
    [Serializable]
    public ref class CRealEstateProperty
    {
    public:
    	CRealEstateProperty(void);
    
    private:
    	String ^ nbr;
    	String ^ type;
    	String ^ adrs;
    	String ^ ct;
    	String ^ stt;
    	String ^ code;
    	short    levels;
    	int      year;
    	short    beds;
    	float    baths;
    	String ^ cond;
    	String ^ status;
    	double   price;
    	String ^ file;
    
    public:
    	property String ^ PropertyNumber
    	{
    		String ^ get() { return nbr; }
    		void set(String ^ value) { nbr = value; }
    	}
            
    	property String ^ PropertyType
    	{
    		String ^ get() { return type; }
    		void set(String ^ value) { type = value; }
    	}
            
    	property String ^ Address
    	{
    		String ^ get() { return adrs; }
    		void set(String ^ value) { adrs = value; }
    	}
            
    	property String ^ City
    	{
    		String ^ get() { return ct; }
    		void set(String ^ value) { ct = value; }
    	}
            
    	property String ^ State
    	{
    		String ^ get() { return stt; }
    		void set(String ^ value) { stt = value; }
    	}
            
    	property String ^ ZIPCode
    	{
    		String ^ get() { return code; }
    		void set(String ^ value) { code = value; }
    	}
            
    	property short Stories
    	{
    		short get() { return levels; }
    		void set(short value) { levels = value; }
    	}
            
    	property int YearBuilt
    	{
    		int get() { return year; }
    		void set(int value) { year = value; }
    	}
            
    	property short Bedrooms
    	{
    		short get() { return beds; }
    		void set(short value) { beds = value; }
    	}
            
    	property float Bathrooms
    	{
    		float get() { return baths; }
    		void set(float value) { baths = value; }
    	}
            
    	property String ^ Condition
    	{
    		String ^ get() { return cond; }
    		void set(String ^ value) { cond = value; }
    	}
            
    	property String ^ SaleStatus
    	{
    		String ^ get() { return status; }
    		void set(String ^ value) { status = value; }
    	}
            
    	property double MarketValue
    	{
    		double get() { return price; }
    		void set(double value) { price = value; }
    	}
            
    	property String ^ PictureFile
    	{
    		String ^ get() { return file; }
    		void set(String ^ value) { file = value; }
    	}
    	
    	virtual bool Equals(Object ^ obj) override;
    	virtual int CRealEstateProperty::GetHashCode() override;
    };
  11. Access the source file and change it as follows:
    #include "StdAfx.h"
    #include "RealEstateProperty.h"
    
    
    CRealEstateProperty::CRealEstateProperty(void)
    {
    }
    
    // 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
    bool CRealEstateProperty::Equals(Object ^ obj)
    {
        CRealEstateProperty ^ rep = reinterpret_cast<CRealEstateProperty ^>(obj);
    	
        if (rep->PropertyNumber == PropertyNumber)
    	return true;
        else
    	return false;
    }
    
    // To avoid a compiler warning
    int CRealEstateProperty::GetHashCode()
    {
    	return GetHashCode();
    }
  12. In the Solution Explorer, right-click Form1.h and click Rename
  13. Type AltairRealtors.h and press Enter twice to display the form
  14. From the Toolbox, add a list view to the form
  15. Right-click that list box on the click Edit Columns...
  16. 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
  17. Click OK
  18. 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
  19. Right-click the form and click Edit Groups...
  20. Create the groups as follows:
     
    Header Name
    Condominium lvgCondominium
    Townhouse lvgTownhouse
    Single Family lvgSingleFamily
  21. Click OK
  22. To create a new form, on the main menu, click Projects -> Add New Item...
  23. In the middle list, make sure Windows Form is selected.
    Set the Name to PropertyEditor
  24. Click Add
  25. In the Dialogs section of the Toolbox, click OpenFileDialog
  26. Click the form
  27. In the Properties window, change its characteristics as follows:
    (Name):      dlgPicture
    DefaultExt:  jpg
    Filter:         JPEG Files (*.jpg,*.jpeg)|*.jpg
    Title:          Select House Picture
  28. 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
  29. Double-click an unoccupied area of the form
  30. Return to the form
  31. Double-click the Picture button
  32. Change the file as follows:
    public:
        bool pictureChanged;
        String ^ pictureFile;
    	
    private:
        System::Void PropertyEditor_Load(System::Object^  sender, System::EventArgs^  e)
        {
            pictureChanged = false;
            pictureFile = L"C:\\Altair Realtors1\000-000.jpg";
        }
    
        System::Void btnPicture_Click(System::Object^  sender, System::EventArgs^  e)
        {
    	if (dlgPicture->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                pbxProperty->Image = Image::FromFile(dlgPicture->FileName);
    	    pictureFile = dlgPicture->FileName;
                pictureChanged = true;
            }
        }
    };
    }

ApplicationPractical Learning: Starting a Linked List Class

  1. In the Solution Explorer, double-click AltairRealtors.h
  2. Double-click an unoccupied area of the form
  3. Change the file as follows:
    #pragma once
    
    #include "RealEstateProperty.h"
    #include "PropertyEditor.h"
    
    namespace AltairRealtors1 {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	using namespace System::IO;
    	using namespace System::Collections::Generic;
    	using namespace System::Runtime::Serialization::Formatters::Binary;
    
    	. . . No Change
    		
    	
    #pragma endregion
    private:
        LinkedList<CRealEstateProperty ^> ^properties;
    	
        void ShowProperties()
        {
    	// Get a reference to the file that holds the records of properties
            String ^ Filename = "C:\\Altair Realtors1\\properties.atr";
    		
    	// Make sure the file exists
            if( File::Exists(Filename) == true )
    	{
    	    // if so, create a file stream
                FileStream ^ stmProperties = gcnew FileStream(Filename,
                                                              FileMode::Open,
                                                              FileAccess::Read);
    	    // Create a binary formatter
                BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter;
    	    // If some properties were created already,
                // get them and store them in the collection
                properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperty->Deserialize(stmProperties));
    			
    	    // First, empty the list view
                lvwProperties->Items->Clear();
                ListViewItem ^lviProperty = nullptr;
    			
    	    // Visit each property in the collection and add it to the list view
                for each( CRealEstateProperty ^ house in properties )
    	    {
    		if( house->PropertyType->Equals(L"Condominium") )
    		    lviProperty = gcnew ListViewItem(house->PropertyNumber, lvwProperties->Groups[0]);
                    else if (house->PropertyType->Equals(L"Townhouse"))
                        lviProperty = gcnew ListViewItem(house->PropertyNumber, lvwProperties->Groups[1]);
                    else // if (house->PropertyType->Equals(L"Single Family"))
                        lviProperty = gcnew ListViewItem(house->PropertyNumber, lvwProperties->Groups[2]);
    				
    		lviProperty->SubItems->Add(house->City);
                    lviProperty->SubItems->Add(house->Stories.ToString());
                    lviProperty->SubItems->Add(house->YearBuilt.ToString());
                    lviProperty->SubItems->Add(house->Bedrooms.ToString());
                    lviProperty->SubItems->Add(house->Bathrooms.ToString(L"F"));
    		lviProperty->SubItems->Add(house->Condition);
                    lviProperty->SubItems->Add(house->SaleStatus);
                    lviProperty->SubItems->Add(house->MarketValue.ToString());
                    lvwProperties->Items->Add(lviProperty);
                }
    
    	    // Close the file stream
    	    stmProperties->Close();
            }
        }
    	
        System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
        {
                properties = gcnew LinkedList<CRealEstateProperty ^>;
                ShowProperties();
        }
    };
    }
  4. Return to the Altair Realtors - Properties Listing form

ApplicationPractical Learning: Adding the First Node

  1. On the form, double-click the New Real Estale Property button
  2. Implement its event as follows:
    System::Void btnNewProperty_Click(System::Object^  sender, System::EventArgs^  e)
    {
        PropertyEditor ^ editor = gcnew PropertyEditor;
    
        Random ^ rndNumber = gcnew Random(DateTime::Now.Millisecond);
        int number1 = rndNumber->Next(100, 999);
        int number2 = rndNumber->Next(100, 999);
        String ^ propNumber = number1 + L"-" + number2;
    
        editor->txtPropertyNumber->Text = propNumber;
    
        // Check that the directory that contains the list of properties exists.
        // If it doesn't exist, create it
        DirectoryInfo  ^ dirInfo = Directory::CreateDirectory(L"C:\\Altair Realtors1");
        // Get a reference to the file that holds the properties
        String ^ Filename = L"C:\\Altair Realtors1\\properties.atr";
    
        // First check if the file was previously created
        if( File::Exists(Filename) == true )
        {
            // If the list of properties exists already,
            // get it and store it in a file stream
            FileStream ^ stmProperties = gcnew FileStream(Filename,
                                                      FileMode::Open,
                                                      FileAccess::Read);
            BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter();
            // Store the list of properties in the collection
            properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperty->Deserialize(stmProperties));
            // Close the file stream
            stmProperties->Close();
        }
    
        if( editor->ShowDialog() == System::Windows::Forms::DialogResult::OK )
        {
            CRealEstateProperty ^ prop = gcnew CRealEstateProperty;
    
            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 = short::Parse(editor->txtStories->Text);
            prop->YearBuilt = int::Parse(editor->txtYearBuilt->Text);
            prop->Bedrooms = short::Parse(editor->txtBedrooms->Text);
            prop->Bathrooms = float::Parse(editor->txtBathrooms->Text);
            prop->Condition = editor->cbxConditions->Text;
            prop->SaleStatus = editor->cbxSaleStatus->Text;
            prop->MarketValue = double::Parse(editor->txtMarketValue->Text);
            if( !editor->pictureFile->Equals(L"") )
            {
                FileInfo ^ flePicture = gcnew FileInfo(editor->pictureFile);
                flePicture->CopyTo(L"C:\\Altair Realtors1\\" +
                                  editor->txtPropertyNumber->Text +
                                  flePicture->Extension);
                prop->PictureFile = L"C:\\Altair Realtors1\\" +
                                  editor->txtPropertyNumber->Text +
                                  flePicture->Extension;
            }
            else
                prop->PictureFile = L"C:\\Altair Realtors1\\000-000.jpg";
            // Add the property in the collection
            properties->AddFirst(prop);
    
            // Get a reference to the properties file
            String ^ strFilename = dirInfo->FullName + "\\properties.atr";
            // Create a file stream to hold the list of properties
            FileStream ^ stmProperties = gcnew FileStream(strFilename,
                                                      FileMode::Create,
                                                      FileAccess::Write);
            BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter();
    
            // Serialize the list of properties
            bfmProperty->Serialize(stmProperties, properties);
            // Close the file stream
            stmProperties->Close();
    
            // Show the list of properties
            ShowProperties();
        }
    }
  3. Return to the form and click the list view
  4. In the Properties window, click Events and double-click ItemSelectionChanged
  5. Implement the event as follows:
    System::Void lvwProperties_ItemSelectionChanged(System::Object^  sender,
    	System::Windows::Forms::ListViewItemSelectionChangedEventArgs^  e)
    {
        CRealEstateProperty ^ currentProperty = gcnew CRealEstateProperty;
    
        for each (CRealEstateProperty ^ prop in properties)
        {
            if (prop->PropertyNumber->Equals(e->Item->SubItems[0]->Text))
                pbxProperty->Image = Image::FromFile(prop->PictureFile);
        }
    }
  6. To execute, press F5
  7. Click the New Real Estate Property button and create a property:

     
    Altair Realtors
  8. Click OK
  9. 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
  10. Create some properties:
     
     
    Altair Realtors
     
    Altair Realtors
  11. Close the form and return to your programming environment
 
 
 

ApplicationPractical Learning: Checking a Node

  1. On the form, click the list view
  2. In the Events section of the Properties window, double-click DoubleClick
  3. Implement its event as follows:
    System::Void lvwProperties_DoubleClick(System::Object^  sender, System::EventArgs^  e)
    {
        if ((lvwProperties->SelectedItems->Count == 0) ||
            (lvwProperties->SelectedItems->Count > 1))
            return;
    
        // Get a reference to the file that holds the properties
        String ^ Filename = L"C:\\Altair Realtors1\\properties.atr";
    
        // Open the file that contains the properties
        FileStream ^ stmProperties = gcnew FileStream(Filename,
                                                  FileMode::Open,
                                                  FileAccess::Read);
        BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter();
        // Store the list of properties in the collection
        properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperty->Deserialize(stmProperties));
        // Close the file stream
        stmProperties->Close();
    
        // Get the property that the user double-clicked
        ListViewItem ^ lviProperty = lvwProperties->SelectedItems[0];
        CRealEstateProperty ^ house = gcnew CRealEstateProperty;
        for each( CRealEstateProperty ^ prop in properties )
            if( prop->PropertyNumber == lviProperty->Text )
                house = prop;
    
        // Get a reference to the property editor
        PropertyEditor ^ editor = gcnew 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 = house->Stories.ToString();
        editor->txtYearBuilt->Text = house->YearBuilt.ToString();
        editor->txtBedrooms->Text = house->Bedrooms.ToString();
        editor->txtBathrooms->Text = house->Bathrooms.ToString(L"F");
        editor->cbxConditions->Text = house->Condition;
        editor->cbxSaleStatus->Text = house->SaleStatus;
        editor->txtMarketValue->Text = house->MarketValue.ToString(L"F");
        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();
    }
  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:
    System::Void lvwProperties_DoubleClick(System::Object^  sender, System::EventArgs^  e)
    {
        if ((lvwProperties->SelectedItems->Count == 0) ||
            (lvwProperties->SelectedItems->Count > 1))
            return;
    
        // Get a reference to the file that holds the properties
        String ^ Filename = L"C:\\Altair Realtors1\\properties.atr";
    
        // Open the file that contains the properties
        FileStream ^ stmProperties = gcnew FileStream(Filename,
                                                  FileMode::Open,
                                                  FileAccess::Read);
        BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter;
        // Store the list of properties in the linked list
        properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperty->Deserialize(stmProperties));
        // Close the file stream
        stmProperties->Close();
    
        // Get the property that the user double-clicked
        ListViewItem ^ lviProperty = lvwProperties->SelectedItems[0];
        CRealEstateProperty ^ house = gcnew CRealEstateProperty;
        // Get the property number the user double-clicked
        house->PropertyNumber = lviProperty->Text;
    
        // Find the property in the list
        LinkedListNode<CRealEstateProperty ^> ^ nodProperty = properties->Find(house);
    
        // Get a reference to the property editor
        PropertyEditor ^ editor = gcnew PropertyEditor();
        
        // Prepare to fill the editor with the values of the property the user double-clicked
        editor->txtPropertyNumber->Text = nodProperty->Value->PropertyNumber; // house->PropertyNumber;
        editor->cbxPropertyTypes->Text = nodProperty->Value->PropertyType;
        editor->txtAddress->Text = nodProperty->Value->Address;
        editor->txtCity->Text = nodProperty->Value->City;
        editor->cbxStates->Text = nodProperty->Value->State;
        editor->txtZIPCode->Text = nodProperty->Value->ZIPCode;
        editor->txtStories->Text = nodProperty->Value->Stories.ToString();
        editor->txtYearBuilt->Text = nodProperty->Value->YearBuilt.ToString();
        editor->txtBedrooms->Text = nodProperty->Value->Bedrooms.ToString();
        editor->txtBathrooms->Text = nodProperty->Value->Bathrooms.ToString(L"F");
        editor->cbxConditions->Text = nodProperty->Value->Condition;
        editor->cbxSaleStatus->Text = nodProperty->Value->SaleStatus;
        editor->txtMarketValue->Text = nodProperty->Value->MarketValue.ToString(L"F");
        editor->pbxProperty->Image = Image::FromFile(nodProperty->Value->PictureFile);
        editor->pictureFile = nodProperty->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)
        {
            // For each value that has changed in the property, update its nnode
            nodProperty->Value->PropertyType = editor->cbxPropertyTypes->Text;
            nodProperty->Value->Address = editor->txtAddress->Text;
            nodProperty->Value->City = editor->txtCity->Text;
            nodProperty->Value->State = editor->cbxStates->Text;
            nodProperty->Value->ZIPCode = editor->txtZIPCode->Text;
            nodProperty->Value->Stories = short::Parse(editor->txtStories->Text);
            nodProperty->Value->YearBuilt = int::Parse(editor->txtYearBuilt->Text);
            nodProperty->Value->Bedrooms = short::Parse(editor->txtBedrooms->Text);
            nodProperty->Value->Bathrooms = float::Parse(editor->txtBathrooms->Text);
            nodProperty->Value->Condition = editor->cbxConditions->Text;
            nodProperty->Value->SaleStatus = editor->cbxSaleStatus->Text;
            nodProperty->Value->MarketValue = double::Parse(editor->txtMarketValue->Text);
            if( editor->pictureChanged == true )
                nodProperty->Value->PictureFile = editor->pictureFile;
            
            // Start saving the linked list
            stmProperties = gcnew FileStream(Filename,
                                           FileMode::OpenOrCreate,
                                           FileAccess::ReadWrite);
            bfmProperty = gcnew BinaryFormatter;
    
            // Serialize the list of properties
            bfmProperty->Serialize(stmProperties, properties);
            // Close the file stream
            stmProperties->Close();
    
            // Show the list of properties
            ShowProperties();
        }
    }
  2. Press F5 to execute
  3. Double-click the second row in the Townhouse section
  4. Change its year built to 2005
  5. Change its market value to 585985
     
    Altair Realtors
  6. Click OK
  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 New Item...
  2. In the middle list, make sure Windows Form is selected.
    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. Double-click an unoccupied area of the form
  6. Change the file as follows:
    #pragma once
    
    #include "RealEstateProperty.h"
    
    namespace AltairRealtors1 {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	using namespace System::IO;
    	using namespace System::Collections::Generic;
    	using namespace System::Runtime::Serialization::Formatters::Binary;
    
    	. . . No Change
    	
    
    
    private:
            int index;
            LinkedList<CRealEstateProperty ^> ^properties;
            LinkedListNode<CRealEstateProperty ^> ^nodCurrent;
    
    System::Void PropertiesReview_Load(System::Object^  sender, System::EventArgs^  e)
    {
        index = 1;
        // Get a reference to the file that holds the records of properties
        String ^ Filename = L"C:\\Altair Realtors1\\properties.atr";
    	
        // Make sure the file exists
        if( File::Exists(Filename) == true )
        {
    	// if so, create a file stream
            FileStream ^ stmProperties = gcnew FileStream(Filename,
                                                          FileMode::Open,
                                                          FileAccess::Read);
    	// Create a binary formatter
            BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter;
    	// If some properties were created already,
            // get them and store them in the collection
    	properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperty->Deserialize(stmProperties));
    		
    	btnFirst_Click(sender, e);
        }
    }
    };
    }
  7. Return to the form and double-click the First button
  8. Implement its event as follows:
    System::Void btnFirst_Click(System::Object^  sender, System::EventArgs^  e)
    {
        index = 1;
        nodCurrent = properties->First;
    
        txtPropertyNumber->Text = nodCurrent->Value->PropertyNumber;
        txtPropertyType->Text = nodCurrent->Value->PropertyType;
        txtAddress->Text = nodCurrent->Value->Address;
        txtCity->Text = nodCurrent->Value->City;
        txtState->Text = nodCurrent->Value->State;
        txtZIPCode->Text = nodCurrent->Value->ZIPCode;
        txtStories->Text = nodCurrent->Value->Stories.ToString();
        txtYearBuilt->Text = nodCurrent->Value->YearBuilt.ToString();
        txtBedrooms->Text = nodCurrent->Value->Bedrooms.ToString();
        txtBathrooms->Text = nodCurrent->Value->Bathrooms.ToString(L"F");
        txtCondition->Text = nodCurrent->Value->Condition;
        txtSaleStatus->Text = nodCurrent->Value->SaleStatus;
        txtMarketValue->Text = nodCurrent->Value->MarketValue.ToString(L"F");
        pbxProperty->Image = Image::FromFile(nodCurrent->Value->PictureFile);
    
        lblRecordNumber->Text = L"1 of " + properties->Count.ToString();
    }
  9. Return to the form and double-click the Previous button
  10. Implement its event as follows:
    System::Void btnPrevious_Click(System::Object^  sender, System::EventArgs^  e)
    {
        index--;
    
        if (index <= 1)
            btnFirst_Click(sender, e);
        else
        {
            LinkedListNode<CRealEstateProperty ^> ^ current = nodCurrent->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 = current->Value->Stories.ToString();
            txtYearBuilt->Text = current->Value->YearBuilt.ToString();
            txtBedrooms->Text = current->Value->Bedrooms.ToString();
            txtBathrooms->Text = current->Value->Bathrooms.ToString(L"F");
            txtCondition->Text = current->Value->Condition;
            txtSaleStatus->Text = current->Value->SaleStatus;
            txtMarketValue->Text = current->Value->MarketValue.ToString(L"F");
    
            lblRecordNumber->Text = index.ToString() + " of " + properties->Count.ToString();
        }
    }
  11. Return to the form and double-click the Next button
  12. Implement its event as follows:
    System::Void btnNext_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	index++;
    
        if (index >= properties->Count)
            btnLast_Click(sender, e);
        else
        {
            LinkedListNode<CRealEstateProperty ^> ^ current = nodCurrent->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 = current->Value->Stories.ToString();
            txtYearBuilt->Text = current->Value->YearBuilt.ToString();
            txtBedrooms->Text = current->Value->Bedrooms.ToString();
            txtBathrooms->Text = current->Value->Bathrooms.ToString(L"F");
            txtCondition->Text = current->Value->Condition;
            txtSaleStatus->Text = current->Value->SaleStatus;
            txtMarketValue->Text = current->Value->MarketValue.ToString(L"F");
            pbxProperty->Image = Image::FromFile(current->Value->PictureFile);
    
            lblRecordNumber->Text = index.ToString() + L" of " + properties->Count.ToString();
        }
    }
  13. Return to the form and double-click the Last button
  14. Implement its event as follows:
    System::Void btnLast_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	index = properties->Count;
        nodCurrent = properties->Last;
    
        txtPropertyNumber->Text = nodCurrent->Value->PropertyNumber;
        txtPropertyType->Text = nodCurrent->Value->PropertyType;
        txtAddress->Text = nodCurrent->Value->Address;
        txtCity->Text = nodCurrent->Value->City;
        txtState->Text = nodCurrent->Value->State;
        txtZIPCode->Text = nodCurrent->Value->ZIPCode;
        txtStories->Text = nodCurrent->Value->Stories.ToString();
        txtYearBuilt->Text = nodCurrent->Value->YearBuilt.ToString();
        txtBedrooms->Text = nodCurrent->Value->Bedrooms.ToString();
        txtBathrooms->Text = nodCurrent->Value->Bathrooms.ToString(L"F");
        txtCondition->Text = nodCurrent->Value->Condition;
        txtSaleStatus->Text = nodCurrent->Value->SaleStatus;
        txtMarketValue->Text = nodCurrent->Value->MarketValue.ToString(L"F");
        pbxProperty->Image = Image::FromFile(nodCurrent->Value->PictureFile);
    
        lblRecordNumber->Text = properties->Count.ToString() + " of " + properties->Count.ToString();
    }
  15. Return to the form and double-click the Close button
  16. Implement it as follows:
    System::Void btnCancel_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	Close();
    }
  17. Display the Properties Listing form
  18. 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...
  19. Double-click the Navigate button and implement its event as follows:
    #pragma once
    
    #include "RealEstateProperty.h"
    #include "PropertyEditor.h"
    #include "PropertiesReview.h"
    
    namespace AltairRealtors1 {
    
    	. . . No Change
    	
    System::Void btnReviewProperties_Click(System::Object^  sender, System::EventArgs^  e)
    {
        PropertiesReview ^ pr = gcnew PropertiesReview;
    
        pr->ShowDialog();
    }
  20. Press F5 to execute
  21. Click the Review Properties button
  22. 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. Under the form, click cmsProperties
  9. Under ContextMenuStrip, double-click New Property...
  10. Implement the event as follows:
    System::Void mnuNewProperty_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	btnNewProperty_Click(sender, e);
    }
  11. Locate the ItemSelectionChanged event and change its implementation as follows:
    System::Void lvwProperties_ItemSelectionChanged(System::Object^  sender,
    	System::Windows::Forms::ListViewItemSelectionChangedEventArgs^  e)
    {
    	CRealEstateProperty ^ currentProperty = gcnew CRealEstateProperty;
    
        for each (CRealEstateProperty ^ prop in properties)
        {
            if (prop->PropertyNumber->Equals(e->Item->SubItems[0]->Text))
                pbxProperty->Image = Image::FromFile(prop->PictureFile);
        }
    
        if( lvwProperties->SelectedItems->Count == 1)
        {
            mnuInsertBefore->Enabled = true;
            mnuEditProperty->Enabled = true;
            mnuInsertAfter->Enabled = true;
        }
        else
        {
            mnuInsertBefore->Enabled = false;
            mnuEditProperty->Enabled = false;
            mnuInsertAfter->Enabled = false;
        }
    }
  12. Return to the form and, under the form, click cmsProperties
  13. On the form, double-click Edit properties
  14. Implement its event as follows:
    System::Void mnuEditProperty_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	lvwProperties_DoubleClick(sender, e);
    }
  15. Return to the form
  16. On the form, double-click Insert Before...
  17. Implement the event as follows:
    System::Void mnuInsertBefore_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	PropertyEditor ^ editor = gcnew PropertyEditor();
        String ^ Filename = L"C:\\Altair Realtors1\\properties.atr";
        DirectoryInfo  ^ dirInfo = Directory::CreateDirectory(L"C:\\Altair Realtors1");
        
        if( File::Exists(Filename) == true )
        {
            FileStream ^ stmProperties = gcnew FileStream(Filename,
                                                      FileMode::Open,
                                                      FileAccess::Read);
            BinaryFormatter ^ bfmProperties = gcnew BinaryFormatter();
    
            properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperties->Deserialize(stmProperties));
            stmProperties->Close();
            stmProperties->Close();
        }
    
        CRealEstateProperty ^ rep = gcnew CRealEstateProperty;
        String ^ strPropertyNumber = lvwProperties->SelectedItems[0]->Text;
        rep->PropertyNumber = lvwProperties->SelectedItems[0]->Text;
    
        Random ^ rndNumber = gcnew Random(DateTime::Now.Millisecond);
        int number1 = rndNumber->Next(100, 999);
        int number2 = rndNumber->Next(100, 999);
        String ^ propNumber = number1 + L"-" + number2;
    
        editor->txtPropertyNumber->Text = propNumber;
        editor->cbxPropertyTypes->Text = lvwProperties->SelectedItems[0]->Group->Header;
        editor->cbxPropertyTypes->Enabled = false;
    
        LinkedListNode<CRealEstateProperty ^> ^ nodProperty = properties->Find(rep);
    
        if( nodProperty != nullptr )
        {
            if (editor->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                CRealEstateProperty ^ prop = gcnew CRealEstateProperty;
    
                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 = short::Parse(editor->txtStories->Text);
                prop->YearBuilt = int::Parse(editor->txtYearBuilt->Text);
                prop->Bedrooms = short::Parse(editor->txtBedrooms->Text);
                prop->Bathrooms = float::Parse(editor->txtBathrooms->Text);
                prop->Condition = editor->cbxConditions->Text;
                prop->SaleStatus = editor->cbxSaleStatus->Text;
                prop->MarketValue = double::Parse(editor->txtMarketValue->Text);
                if( !editor->pictureFile->Equals(L"") )
                {
                    FileInfo ^ flePicture = gcnew FileInfo(editor->pictureFile);
                    flePicture->CopyTo(L"C:\\Altair Realtors1\\" +
                                      editor->txtPropertyNumber->Text +
                                      flePicture->Extension);
                    prop->PictureFile = L"C:\\Altair Realtors1\\" +
                                      editor->txtPropertyNumber->Text +
                                      flePicture->Extension;
                }
                else
                    prop->PictureFile = L"C:\\Altair Realtors1\\000-000.jpg";
    
                // Insert the property before the currently selected node
                properties->AddBefore(nodProperty, prop);
    
                // Get a reference to the properties file
                String ^ strFilename = dirInfo->FullName + "\\properties.atr";
                // Create a file stream to hold the list of properties
                FileStream ^ stmProperties = gcnew FileStream(strFilename,
                                                          FileMode::Create,
                                                          FileAccess::Write);
                BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter();
    
                // Serialize the list of properties
                bfmProperty->Serialize(stmProperties, properties);
                // Close the file stream
                stmProperties->Close();
    
                // Show the list of properties
                ShowProperties();
            }
        }
    }
  18. Return to the form
  19. On the form, double-click Insert After...
  20. Implement the event as follows:
    System::Void mnuInsertAfter_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	PropertyEditor ^ editor = gcnew PropertyEditor();
        String ^ Filename = L"C:\\Altair Realtors1\\properties.atr";
        DirectoryInfo  ^ dirInfo = Directory::CreateDirectory(L"C:\\Altair Realtors1");
    
        if( File::Exists(Filename) == true )
        {
            FileStream ^ stmProperties = gcnew FileStream(Filename,
                                                      FileMode::Open,
                                                      FileAccess::Read);
            BinaryFormatter ^ bfmProperties = gcnew BinaryFormatter();
    		
            properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperties->Deserialize(stmProperties));
            // Close the file stream
            stmProperties->Close();
            stmProperties->Close();
        }
    
        CRealEstateProperty ^ rep = gcnew CRealEstateProperty;
        String ^ strPropertyNumber = lvwProperties->SelectedItems[0]->Text;
        rep->PropertyNumber = lvwProperties->SelectedItems[0]->Text;
    
        Random ^ rndNumber = gcnew Random(DateTime::Now.Millisecond);
        int number1 = rndNumber->Next(100, 999);
        int number2 = rndNumber->Next(100, 999);
        String ^ propNumber = number1 + L"-" + number2;
    
        editor->txtPropertyNumber->Text = propNumber;
        editor->cbxPropertyTypes->Text = lvwProperties->SelectedItems[0]->Group->Header;
        editor->cbxPropertyTypes->Enabled = false;
    
        LinkedListNode<CRealEstateProperty ^> ^ nodProperty = properties->Find(rep);
    
        if( nodProperty != nullptr )
        {
            if (editor->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                CRealEstateProperty ^ prop = gcnew CRealEstateProperty;
    
                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 = short::Parse(editor->txtStories->Text);
                prop->YearBuilt = int::Parse(editor->txtYearBuilt->Text);
                prop->Bedrooms = short::Parse(editor->txtBedrooms->Text);
                prop->Bathrooms = float::Parse(editor->txtBathrooms->Text);
                prop->Condition = editor->cbxConditions->Text;
                prop->SaleStatus = editor->cbxSaleStatus->Text;
                prop->MarketValue = double::Parse(editor->txtMarketValue->Text);
                if( !editor->pictureFile->Equals(L"") )
                {
                    FileInfo ^ flePicture = gcnew FileInfo(editor->pictureFile);
                    flePicture->CopyTo(L"C:\\Altair Realtors1\\" +
                                      editor->txtPropertyNumber->Text +
                                      flePicture->Extension);
                    prop->PictureFile = L"C:\\Altair Realtors1\\" +
                                      editor->txtPropertyNumber->Text +
                                      flePicture->Extension;
                }
                else
                    prop->PictureFile = L"C:\\Altair Realtors1\\000-000.jpg";
    
                // Insert the property before the currently selected node
                properties->AddAfter(nodProperty, prop);
    
                // Get a reference to the properties file
                String ^ strFilename = dirInfo->FullName + "\\properties.atr";
                // Create a file stream to hold the list of properties
                FileStream ^ stmProperties = gcnew FileStream(strFilename,
                                                          FileMode::Create,
                                                          FileAccess::Write);
                BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter();
    
                // Serialize the list of properties
                bfmProperty->Serialize(stmProperties, properties);
                // Close the file stream
                stmProperties->Close();
    
                // Show the list of properties
                ShowProperties();
            }
        }
    }
  21. To execute, press F5
  22. Create the properties (let the application generate the property number)
  23. 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. Double-click the Delete Property menu item
  5. In the file, locate the ItemSelectionChanged event and change its implementation as follows:
    System::Void lvwProperties_ItemSelectionChanged(System::Object^  sender,
    	System::Windows::Forms::ListViewItemSelectionChangedEventArgs^  e)
    {
    	CRealEstateProperty ^ currentProperty = gcnew CRealEstateProperty;
    
        for each (CRealEstateProperty ^ prop in properties)
        {
            if (prop->PropertyNumber->Equals(e->Item->SubItems[0]->Text))
                pbxProperty->Image = Image::FromFile(prop->PictureFile);
        }
    
        if( lvwProperties->SelectedItems->Count == 1)
        {
            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;
        }
    }
  6. Scroll down and implement the new event as follows:
    System::Void mnuDeleteProperty_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	PropertyEditor ^ editor = gcnew PropertyEditor();
        String ^ Filename = L"C:\\Altair Realtors1\\properties.atr";
        DirectoryInfo ^ dirInfo = Directory::CreateDirectory(L"C:\\Altair Realtors1");
    
        // Open the file that holds the properties
        if( File::Exists(Filename) == true )
        {
            FileStream ^ stmProperties = gcnew FileStream(Filename,
                                                      FileMode::Open,
                                                      FileAccess::Read);
            BinaryFormatter ^ bfmProperties = gcnew BinaryFormatter;
    
            properties = reinterpret_cast<LinkedList<CRealEstateProperty ^> ^>(bfmProperties->Deserialize(stmProperties));
            stmProperties->Close();
            stmProperties->Close();
        }
    
        // Create a real estate property using the property number that the user double-clicked
        CRealEstateProperty ^ rep = gcnew CRealEstateProperty;
        String ^ strPropertyNumber = lvwProperties->SelectedItems[0]->Text;
        rep->PropertyNumber = lvwProperties->SelectedItems[0]->Text;
        
        // Ask the compiler to locate that property
        LinkedListNode<CRealEstateProperty ^> ^ nodProperty = properties->Find(rep);
    
        // Just in case, make sure the property was found
        if( nodProperty != nullptr )
        {
            // Present a warning message to the user
            if( MessageBox::Show(L"Are you sure you want to delete this property?",
                                L"Altair Realtors",
                                MessageBoxButtons::YesNoCancel,
                                MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes )
            {
                // If the user clicks yes, delete the property
                properties->Remove(nodProperty);
    
                // Save the list of properties
                String ^ strFilename = dirInfo->FullName + "\\properties.atr";
                FileStream ^ stmProperties = gcnew FileStream(strFilename,
                                                          FileMode::Create,
                                                          FileAccess::Write);
                BinaryFormatter ^ bfmProperty = gcnew BinaryFormatter();
    
                bfmProperty->Serialize(stmProperties, properties);
                stmProperties->Close();
    
                // Show the new list of properties
                ShowProperties();
            }
        }
    }
  7. Return to the form and double-click the Close button
  8. Implement it as follows:
    System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	Close();
    }
  9. To execute, press F5
  10. Right-click one of the records and click Delete Property
  11. Click No
  12. Right-click another row and click Delete Property
  13. Click Yes
  14. Close the form and return to your programming environment
 
 
   
 

Home Copyright © 2010-2016, FunctionX