Collection-Based Application: Altair Realtors: Topic Applied: Introducing The Collection Class
Collection-Based Application: Altair Realtors: Topic Applied: Introducing The Collection Class
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.
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; }
if (rep.PropertyNumber == PropertyNumber)
return true;
else
return false;
}
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
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:
Label City:
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:
Label Stories:
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();
}
namespace AltairRealtors10
{
public partial class AltairRealtors : Form
{
LinkedList<RealEstateProperty> properties;
public AltairRealtors()
{
InitializeComponent();
}
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);
}
editor.txtPropertyNumber.Text = propNumber;
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);
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
// 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;
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
TextBox txtPropertyType
Label Address:
TextBox txtAddress
Label City:
TextBox txtCity
Label State:
TexBox txtState
TextBox txtZIPCode
Label Stories:
TextBox txtStories
TextBox txtYearBuilt
Label Condition:
TextBox txtCondition
Label Bedrooms:
TextBox txtBedrooms 0
Label Bathrooms:
TextBox txtStatus
BorderStyle:
PictureBox pbxProperty FixedSingle
SizeMode: Zoom
Button btnClose Close
namespace AltairRealtors10
{
public partial class PropertiesReview : Form
{
int index;
LinkedList<RealEstateProperty> properties;
LinkedListNode<RealEstateProperty> nodCurrent;
public PropertiesReview()
{
InitializeComponent();
}
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);
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++;
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:
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();
}
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";
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();
}
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";
FileAccess.Write);
BinaryFormatter bfmProperty = new BinaryFormatter();
if (lvwProperties.SelectedItems.Count == 1)
lviSelected = lvwProperties.SelectedItems[0];
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;
}
}
properties =
(LinkedList<RealEstateProperty>)bfmProperties.Deserialize(stmPropertie
s);
stmProperties.Close();
stmProperties.Close();
}
bfmProperty.Serialize(stmProperties, properties);
stmProperties.Close();