0% found this document useful (0 votes)
45 views

Collection-Based Application: Altair Realtors: Topic Applied: Introducing The Collection Class

1. The document describes the creation of a Windows Forms application called Altair Realtors that allows users to view and edit a collection of real estate property listings. 2. A RealEstateProperty class is created to represent individual property objects with properties like the address, number of bedrooms, sale status, etc. 3. The main form displays properties in groups and columns, and allows the user to add new properties or view/edit existing ones in a separate PropertyEditor form.

Uploaded by

Antony Raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Collection-Based Application: Altair Realtors: Topic Applied: Introducing The Collection Class

1. The document describes the creation of a Windows Forms application called Altair Realtors that allows users to view and edit a collection of real estate property listings. 2. A RealEstateProperty class is created to represent individual property objects with properties like the address, number of bedrooms, sale status, etc. 3. The main form displays properties in groups and columns, and allows the user to add new properties or view/edit existing ones in a separate PropertyEditor form.

Uploaded by

Antony Raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

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.

Topic Applied: 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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AltairRealtors1
{
[Serializable]
public class RealEstateProperty
{
public string PropertyNumber { get; set; }
public string PropertyType { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZIPCode { get; set; }
public short Stories { get; set; }
public int YearBuilt { get; set; }
public short Bedrooms { get; set; }
public float Bathrooms { get; set; }
public string Condition { get; set; }
public string SaleStatus { get; set; }
public double MarketValue { get; set; }
public string PictureFile { get; set; }

// 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 override bool Equals(object obj)
{
RealEstateProperty rep = (RealEstateProperty)obj;

if (rep.PropertyNumber == PropertyNumber)
return true;
else
return false;
}

// To avoid a compiler warning


public override int GetHashCode()
{
return base.GetHashCode();
}
}
}
10. In the Solution Explorer, right-click Form1.cs and click Rename
11. Type AltairRealtors.cs 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:
Control (Name) Anchor BorderStyle SizeMode  Text 
Top, Bottom,
ListView lvwProperties      
Left, Right
New Real Estate
Button btnNewProperty Bottom, Left    
Property... 
PictureBox pbxPicture Bottom, Right FixedSingle  Zoom   

Button btnClose Bottom, Right      Close

Form
Text: Altair Realtors - Properties Listing
StartPosition: CenterScreen
1. Right-click the form and click Edit Groups...
2. Create the groups as follows:
Header Name
Condominium lvgCondominium
Townhouse lvgTownhouse
Single Family lvgSingleFamily
3. Click OK
4. To create a new form, on the main menu, click Projects -> Add Windows Form...
5. Set the Name to PropertyEditor
6. Click Add
7. In the Dialogs section of the Toolbox, click OpenFileDialog
8. Click the form
9. In the Properties window, change its characteristics as follows:
(Name):      dlgPicture
DefaultExt:  jpg
Filter:         JPEG Files (*.jpg,*.jpeg)|*.jpg
Title:          Select House Picture
10. Design the form as follows:
DropDownSty Modifier Other
Control (Name) Text Items
le s Properties
Property
Label          
Type:
Condominiu
m
ComboBo cbxPropertyType DropDownLis Townhouse
  Public  
x s t Single
Family
Unknown
Property
Label          
#:
txtPropertyNum
TextBox       Public  
ber
Label     Address:      

TextBox txtAddress       Public  

Label     City:      

TextBox txtCity       Public  

Label     State:      
AL, AK, AZ,
AR, CA, CO,
CT, DE, DC,
FL, GA, HI,
ID, IL, IN,
IA, KS, KY,
LA, ME, MD,
MA, MI, MN,
ComboBo DropDownLis MS, MO,
cbxStates   Public  
x t 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
Label     ZIP Code:      

TextBox txtZIPCode       Public  

Label     Stories:      

TextBox txtStories       Public  

Label     Year Built:      


TextBox txtYearBuilt       Public  

Label     Condition:      
Excellent
ComboBo DropDownLis Good Shape
cbxConditions   Public  
x t  Needs
Fixing
Bedrooms
Label          
:
TextBox txtBedrooms   0   Public  
Bathroom
Label          
s:
TextBox txtBathrooms   0.00   Public  
Market
Label          
Value:
TextBox txtMarketValue   0.00   Public  
Sale
Label          
Status:
Unspecified
ComboBo DropDownLis
cbxSaleStatus   Available Public  
x t
Sold
Button btnPicture   Picture...      
BorderStyle
:
PictureBo
pbxProperty         FixedSingle
x
SizeMode:
Zoom
DialogResul
Button btnOK   OK    
t: OK
DialogResul
Button btnCancel   Cancel    
t: Cancel

Form
FormBorderStyle: FixedDialog
Text: Altair Realtors - Property Editor
StartPosition: CenterScreen
AcceptButton: btnOK
CancelButton: btnCancel
MaximizeBox: False
MinimizeBox: False
ShowInTaskBar: False
11. Double-click an unoccupied area of the form
12. Return to the form
13. Double-click the Picture button
14. Change the file as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AltairRealtors1
{
public partial class PropertyEditor : Form
{
public bool pictureChanged;
public string pictureFile;

public PropertyEditor()
{
InitializeComponent();
}

private void PropertyEditor_Load(object sender,


EventArgs e)
{
pictureChanged = false;
pictureFile = "C:\\Altair Realtors1\\000-000.jpg";
}

private void btnPicture_Click(object sender, EventArgs


e)
{
if (dlgPicture.ShowDialog() == DialogResult.OK)
{
pbxProperty.Image =
Image.FromFile(dlgPicture.FileName);
pictureFile = dlgPicture.FileName;
pictureChanged = true;
}
}
}
}

Topic Applied: Starting a Linked List Class

1. In the Solution Explorer, double-click AltairRealtors.cs


2. Double-click an unoccupied area of the form
3. Change the file as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace AltairRealtors10
{
public partial class AltairRealtors : Form
{
LinkedList<RealEstateProperty> properties;

public AltairRealtors()
{
InitializeComponent();
}

private 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 = new FileStream(Filename,
FileMode.Open,
FileAccess.Read);
// Create a binary formatter
BinaryFormatter bfmProperty = new BinaryFormatter();
// If some properties were created already,
// get them and store them in the collection
properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmProperties);

// First, empty the list view


lvwProperties.Items.Clear();
ListViewItem lviProperty = null;

// Visit each property in the collection and add it to the


list view
foreach (RealEstateProperty house in properties)
{
if (house.PropertyType.Equals("Condominium"))
lviProperty = new ListViewItem(house.PropertyNumber,
lvwProperties.Groups[0]);
else if (house.PropertyType.Equals("Townhouse"))
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(house.Stories.ToString());
lviProperty.SubItems.Add(house.YearBuilt.ToString());
lviProperty.SubItems.Add(house.Bedrooms.ToString());
lviProperty.SubItems.Add(house.Bathrooms.ToString("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();
}
}

private void AltairRealtors_Load(object sender, EventArgs e)


{
properties = new LinkedList<RealEstateProperty>();
ShowProperties();
}
}
}
4. Return to the Altair Realtors - Properties Listing form

Topic Applied: Adding the First Node


1. On the form, double-click the New Real Estale Property button
2. Implement its event as follows:
private void btnNewProperty_Click(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();

Random rndNumber = new Random(DateTime.Now.Millisecond);


int number1 = rndNumber.Next(100, 999);
int number2 = rndNumber.Next(100, 999);
string propNumber = number1 + "-" + 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("C:\\Altair
Realtors1");
// Get a reference to the file that holds the properties
string Filename = "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 = new FileStream(Filename,
FileMode.Open,
FileAccess.Read);
BinaryFormatter bfmProperty = new BinaryFormatter();
// Store the list of properties in the collection
properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmP
roperties);
// Close the file stream
stmProperties.Close();
}

if (editor.ShowDialog() == DialogResult.OK)
{
RealEstateProperty prop = 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 = 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(""))
{
FileInfo flePicture = new FileInfo(editor.pictureFile);
flePicture.CopyTo("C:\\Altair Realtors1\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension);
prop.PictureFile = "C:\\Altair Realtors1\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension;
}
else
prop.PictureFile = "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 = new FileStream(strFilename,
FileMode.Create,
FileAccess.Write);
BinaryFormatter bfmProperty = new 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:
private void lvwProperties_ItemSelectionChanged(object sender,
ListViewItemSelectionChangedEventArgs e)
{
RealEstateProperty currentProperty = new RealEstateProperty();

foreach (RealEstateProperty 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

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):
10. Create the following properties
11. Close the form and return to your programming environment
Topic Applied: Checking a Node

10. On the form, click the list view


11. In the Events section of the Properties window, double-click DoubleClick
12. Implement its event as follows:
13. On the form, click the list view
14. In the Events section of the Properties window, double-click DoubleClick
15. Implement its event as follows:
private void lvwProperties_DoubleClick(object sender, EventArgs e)
{
if ((lvwProperties.SelectedItems.Count == 0) ||
(lvwProperties.SelectedItems.Count > 1))
return;

// Get a reference to the file that holds the properties


string Filename = "C:\\Altair Realtors1\\properties.atr";

// Open the file that contains the properties


FileStream stmProperties = new FileStream(Filename,
FileMode.Open,
FileAccess.Read);
BinaryFormatter bfmProperty = new BinaryFormatter();
// Store the list of properties in the collection
properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmPrope
rties);
// Close the file stream
stmProperties.Close();

// Get the property that the user double-clicked


var lviProperty = lvwProperties.SelectedItems[0];
RealEstateProperty house = new RealEstateProperty();
foreach( RealEstateProperty prop in properties )
if( prop.PropertyNumber == lviProperty.Text )
house = prop;

// Get a reference to the property editor


PropertyEditor editor = 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 = house.Stories.ToString();
editor.txtYearBuilt.Text = house.YearBuilt.ToString();
editor.txtBedrooms.Text = house.Bedrooms.ToString();
editor.txtBathrooms.Text = house.Bathrooms.ToString("F");
editor.cbxConditions.Text = house.Condition;
editor.cbxSaleStatus.Text = house.SaleStatus;
editor.txtMarketValue.Text = house.MarketValue.ToString("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

Topic Applied: Updating a Node


1. Change the DoubleClick event as follows:

private void lvwProperties_DoubleClick(object sender, EventArgs e)


{
if ((lvwProperties.SelectedItems.Count == 0) ||
(lvwProperties.SelectedItems.Count > 1))
return;

// Get a reference to the file that holds the properties


string Filename = "C:\\Altair Realtors1\\properties.atr";

// Open the file that contains the properties


FileStream stmProperties = new FileStream(Filename,
FileMode.Open,
FileAccess.Read);
BinaryFormatter bfmProperty = new BinaryFormatter();
// Store the list of properties in the linked list
properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmProperties);
// Close the file stream
stmProperties.Close();

// Get the property that the user double-clicked


var lviProperty = lvwProperties.SelectedItems[0];
RealEstateProperty house = new RealEstateProperty();
// Get the property number the user double-clicked
house.PropertyNumber = lviProperty.Text;

// Find the property in the list


LinkedListNode<RealEstateProperty> nodProperty =
properties.Find(house);

// Get a reference to the property editor


PropertyEditor editor = new PropertyEditor();

// Prepare to fill the editor with the values of the property the
user double-clicked
editor.txtPropertyNumber.Text = nodProperty.Value.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("F");
editor.cbxConditions.Text = nodProperty.Value.Condition;
editor.cbxSaleStatus.Text = nodProperty.Value.SaleStatus;
editor.txtMarketValue.Text =
nodProperty.Value.MarketValue.ToString("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);
nodProperty.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();
}
}
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)

6. Click OK
7. Close the form and return to your programming environment
Topic Applied: 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:
Control (Name) DropDownStyle Text Other Properties
Label     Property #:  

TextBox txtPropertyNumber      

Label     Property Type:  

TextBox txtPropertyType      

Label     Address:  

TextBox txtAddress      

Label     City:  

TextBox txtCity      

Label     State:  

TexBox txtState      

Label     ZIP Code:  

TextBox txtZIPCode      

Label     Stories:  

TextBox txtStories      

Label     Year Built:  

TextBox txtYearBuilt      

Label     Condition:  

TextBox txtCondition      

Label     Bedrooms:  

TextBox txtBedrooms   0  

Label     Bathrooms:  

TextBox txtBathrooms   0.00  

Label     Market Value:  

TextBox txtMarketValue   0.00  

Label     Sale Status:  

TextBox txtStatus      
BorderStyle:
PictureBox pbxProperty     FixedSingle
SizeMode: Zoom
Button btnClose   Close  

Button btnFirst   First  

Label lblRecordNumber   000 of 000  

Button btnPrevious   Previous  

Button btnLast   Last  

5. Double-click an unoccupied area of the form


6. Change the file as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace AltairRealtors10
{
public partial class PropertiesReview : Form
{
int index;
LinkedList<RealEstateProperty> properties;
LinkedListNode<RealEstateProperty> nodCurrent;

public PropertiesReview()
{
InitializeComponent();
}

private void PropertiesReview_Load(object sender,


EventArgs e)
{
index = 1;
// 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 = new
FileStream(Filename,
FileMode.Open,

FileAccess.Read);
// Create a binary formatter
BinaryFormatter bfmProperty = new
BinaryFormatter();
// If some properties were created already,
// get them and store them in the collection
properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmPropert
ies);

btnFirst_Click(sender, e);
}
}
}
}
7. Return to the form and double-click the First button
8. Implement its event as follows:
private void btnFirst_Click(object sender, 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("F");
txtCondition.Text = nodCurrent.Value.Condition;
txtSaleStatus.Text = nodCurrent.Value.SaleStatus;
txtMarketValue.Text = nodCurrent.Value.MarketValue.ToString("F");
pbxProperty.Image = Image.FromFile(nodCurrent.Value.PictureFile);

lblRecordNumber.Text = "1 of " + properties.Count.ToString();


}
9. Return to the form and double-click the Previous button
10. Implement its event as follows:

private void btnPrevious_Click(object sender, EventArgs e)


{
index--;
if (index <= 1)
btnFirst_Click(sender, e);
else
{
LinkedListNode<RealEstateProperty> 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("F");
txtCondition.Text = current.Value.Condition;
txtSaleStatus.Text = current.Value.SaleStatus;
txtMarketValue.Text = current.Value.MarketValue.ToString("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:
private void btnNext_Click(object sender, EventArgs e)
{
index++;

if (index >= properties.Count)


btnLast_Click(sender, e);
else
{
LinkedListNode<RealEstateProperty> 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("F");

txtCondition.Text = current.Value.Condition;

txtSaleStatus.Text = current.Value.SaleStatus;
txtMarketValue.Text = current.Value.MarketValue.ToString("F");
pbxProperty.Image = Image.FromFile(current.Value.PictureFile);

lblRecordNumber.Text = index.ToString() +
" of " + properties.Count.ToString();
}
}
13. Return to the form and double-click the Last button
14. Implement its event as follows:
private void btnLast_Click(object sender, 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("F");
txtCondition.Text = nodCurrent.Value.Condition;
txtSaleStatus.Text = nodCurrent.Value.SaleStatus;
txtMarketValue.Text =
nodCurrent.Value.MarketValue.ToString("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. Type 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:br>(Name): btnReviewProperties
Text: Review Properties...
19. Double-click the Navigate button and implement its event as follows:

private void btnReviewProperties_Click(object sender, EventArgs e)


{
PropertiesReview pr = new PropertiesReview();
pr.ShowDialog();
}
20.Press F5 to execute
21. Click the Review Properties button
22. Close the forms and return to your programming environment
Topic Applied: 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:
private void mnuNewProperty_Click(object sender, EventArgs e)
{
btnNewProperty_Click(sender, e);
}
11. Locate the ItemSelectionChanged event and change its implementation as
follows:

private void lvwProperties_ItemSelectionChanged(object sender,


ListViewItemSelectionChangedEventArgs e)
{
RealEstateProperty currentProperty = new RealEstateProperty();
if (lvwProperties.SelectedItems.Count == 1)
lviSelected = lvwProperties.SelectedItems[0];

foreach (RealEstateProperty 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:
private void mnuEditProperty_Click(object sender, EventArgs e)
{
lvwProperties_DoubleClick(sender, e);
}
15. Return to the form and, on the form, double-click Insert Before...
16. Implement the event as follows:
private void mnuInsertBefore_Click(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();
string Filename = "C:\\Altair Realtors1\\properties.atr";
DirectoryInfo dirInfo = Directory.CreateDirectory("C:\\Altair
Realtors1");

if (File.Exists(Filename) == true)

{
FileStream stmProperties = new FileStream(Filename,
FileMode.Open,
FileAccess.Read);
BinaryFormatter bfmProperties = new BinaryFormatter();

properties =
(LinkedList<RealEstateProperty>)bfmProperties.Deserialize(stmProperties)
;
stmProperties.Close();
stmProperties.Close();
}

RealEstateProperty rep = new RealEstateProperty();


string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;

Random rndNumber = new Random(DateTime.Now.Millisecond);


int number1 = rndNumber.Next(100, 999);
int number2 = rndNumber.Next(100, 999);
string propNumber = number1 + "-" + number2;

editor.txtPropertyNumber.Text = propNumber;
editor.cbxPropertyTypes.Text =
lvwProperties.SelectedItems[0].Group.Header;
editor.cbxPropertyTypes.Enabled = false;

LinkedListNode<RealEstateProperty> nodProperty =
properties.Find(rep);

if (nodProperty != null)
{
if (editor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
RealEstateProperty prop = 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 = 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(""))

{
FileInfo flePicture = new FileInfo(editor.pictureFile);
flePicture.CopyTo("C:\\Altair Realtors1\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension);
prop.PictureFile = "C:\\Altair Realtors1\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension;
}
else
prop.PictureFile = "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 = new FileStream(strFilename,
FileMode.Create,
FileAccess.Write);
BinaryFormatter bfmProperty = new BinaryFormatter();

// Serialize the list of properties


bfmProperty.Serialize(stmProperties, properties);
// Close the file stream
stmProperties.Close();

// Show the list of properties


ShowProperties();
}
}
}
17. Return to the form and, on the form, double-click Insert After...
18. Implement the event as follows:

private void mnuInsertAfter_Click(object sender, EventArgs e)


{
PropertyEditor editor = new PropertyEditor();
string Filename = "C:\\Altair Realtors1\\properties.atr";
DirectoryInfo dirInfo = Directory.CreateDirectory("C:\\Altair
Realtors1");

if (File.Exists(Filename) == true)
{
FileStream stmProperties = new FileStream(Filename,
FileMode.Open,
FileAccess.Read);
BinaryFormatter bfmProperties = new BinaryFormatter();

properties =
(LinkedList<RealEstateProperty>)bfmProperties.Deserialize(stmPropertie
s);
// Close the file stream
stmProperties.Close();
stmProperties.Close();
}

RealEstateProperty rep = new RealEstateProperty();


string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;

Random rndNumber = new Random(DateTime.Now.Millisecond);


int number1 = rndNumber.Next(100, 999);
int number2 = rndNumber.Next(100, 999);
string propNumber = number1 + "-" + number2;

editor.txtPropertyNumber.Text = propNumber;
editor.cbxPropertyTypes.Text =
lvwProperties.SelectedItems[0].Group.Header;
editor.cbxPropertyTypes.Enabled = false;

LinkedListNode<RealEstateProperty> nodProperty =
properties.Find(rep);

if (nodProperty != null)
{
if (editor.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
{
RealEstateProperty prop = 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 = 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(""))
{
FileInfo flePicture = new
FileInfo(editor.pictureFile);
flePicture.CopyTo("C:\\Altair Realtors1\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension);
prop.PictureFile = "C:\\Altair Realtors1\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension;
}
else
prop.PictureFile = "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 = new FileStream(strFilename,
FileMode.Create,

FileAccess.Write);
BinaryFormatter bfmProperty = new BinaryFormatter();

// Serialize the list of properties


bfmProperty.Serialize(stmProperties, properties);
// Close the file stream
stmProperties.Close();

// Show the list of properties


ShowProperties();
}
}
}
19. To execute, press F5
20. Create the properties (let the application generate the property number) (the
new records are in bold):
 
21. Close the form and return to your programming environment

Topic Applied: 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:
private void lvwProperties_ItemSelectionChanged(object sender,
ListViewItemSelectionChangedEventArgs e)
{
RealEstateProperty currentProperty = new RealEstateProperty();

if (lvwProperties.SelectedItems.Count == 1)
lviSelected = lvwProperties.SelectedItems[0];

foreach (RealEstateProperty 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:


private void mnuDeleteProeprty_Click(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();
string Filename = "C:\\Altair Realtors1\\properties.atr";
DirectoryInfo dirInfo = Directory.CreateDirectory("C:\\Altair
Realtors1");

// Open the file that holds the properties


if (File.Exists(Filename) == true)
{
FileStream stmProperties = new FileStream(Filename,
FileMode.Open,
FileAccess.Read);
BinaryFormatter bfmProperties = new BinaryFormatter();

properties =
(LinkedList<RealEstateProperty>)bfmProperties.Deserialize(stmPropertie
s);
stmProperties.Close();
stmProperties.Close();
}

// Create a real estate property using the property number that


the user double-clicked
RealEstateProperty rep = new RealEstateProperty();
string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;

// Ask the compiler to locate that property


LinkedListNode<RealEstateProperty> nodProperty =
properties.Find(rep);

// Just in case, make sure the property was found


if (nodProperty != null)
{
// Present a warning message to the user
if( MessageBox.Show("Are you sure you want to delete this
property?",
"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 = new FileStream(strFilename,


FileMode.Create,
FileAccess.Write);
BinaryFormatter bfmProperty = new 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:
private void btnClose_Click(object sender, 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

You might also like