|
Collection-Based Application: Altair Realtors |
|
|
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.
|
Practical Learning: Introducing the Collection Class
|
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New
Project...
- In the middle list, click Windows Forms Application
- Change the Name to AltairRealtors1
- Click OK
- To create a new class, on the main menu, click Project -> Add
Class...
- Set the Name to RealEstateProperty
- Click Add
- 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();
}
}
}
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type AltairRealtors.cs and press Enter twice to
display the form
- From the Toolbox, add a list view to the form
- Right-click that list box on the click Edit Columns...
- 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 |
- Click OK
- Design the form as follows:
|
Control |
(Name) |
Anchor |
BorderStyle |
SizeMode |
Text |
ListView |
|
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 |
- Right-click the form and click Edit Groups...
- Create the groups as follows:
Header |
Name |
Condominium |
lvgCondominium |
Townhouse |
lvgTownhouse |
Single Family |
lvgSingleFamily |
- Click OK
- To create a new form, on the main menu, click Projects -> Add
Windows Form...
- Set the Name to PropertyEditor
- Click Add
- In the Dialogs section of the Toolbox, click OpenFileDialog
- Click the form
- In the Properties window, change its characteristics as follows:
(Name): dlgPicture DefaultExt: jpg Filter: JPEG
Files (*.jpg,*.jpeg)|*.jpg Title: Select House Picture
- Design the form as follows:
|
Control |
(Name) |
DropDownStyle |
Text |
Items |
Modifiers |
Other Properties |
Label |
|
|
|
Property Type: |
|
|
|
ComboBox |
|
cbxPropertyTypes |
DropDownList |
|
Condominium Townhouse Single Family Unknown |
Public |
|
Label |
|
|
|
Property #: |
|
|
|
TextBox |
|
txtPropertyNumber |
|
|
|
Public |
|
Label |
|
|
|
Address: |
|
|
|
TextBox |
|
txtAddress |
|
|
|
Public |
|
Label |
|
|
|
City: |
|
|
|
TextBox |
|
txtCity |
|
|
|
Public |
|
Label |
|
|
|
State: |
|
|
|
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 |
|
|
|
ZIP Code: |
|
|
|
TextBox |
|
txtZIPCode |
|
|
|
Public |
|
Label |
|
|
|
Stories: |
|
|
|
TextBox |
|
txtStories |
|
|
|
Public |
|
Label |
|
|
|
Year Built: |
|
|
|
TextBox |
|
txtYearBuilt |
|
|
|
Public |
|
Label |
|
|
|
Condition: |
|
|
|
ComboBox |
|
cbxConditions |
DropDownList |
|
Excellent Good Shape Needs Fixing |
Public |
|
Label |
|
|
|
Bedrooms: |
|
|
|
TextBox |
|
txtBedrooms |
|
0 |
|
Public |
|
Label |
|
|
|
Bathrooms: |
|
|
|
TextBox |
|
txtBathrooms |
|
0.00 |
|
Public |
|
Label |
|
|
|
Market Value: |
|
|
|
TextBox |
|
txtMarketValue |
|
0.00 |
|
Public |
|
Label |
|
|
|
Sale Status: |
|
|
|
ComboBox |
|
cbxSaleStatus |
DropDownList |
|
Unspecified Available Sold |
Public |
|
Button |
|
btnPicture |
|
Picture... |
|
|
|
PictureBox |
|
pbxProperty |
|
|
|
|
BorderStyle: FixedSingle SizeMode: Zoom |
Button |
|
btnOK |
|
OK |
|
|
DialogResult: OK |
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 |
- Double-click an unoccupied area of the form
- Return to the form
- Double-click the Picture button
- 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;
}
}
}
}
Practical Learning: Starting a Linked List Class
|
|
- In the Solution Explorer, double-click AltairRealtors.cs
- Double-click an unoccupied area of the form
- 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();
}
}
}
- Return to the Altair Realtors - Properties Listing form
Practical Learning: Adding the First Node
|
|
- On the form, double-click the New Real Estale Property button
- 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(stmProperties);
// 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();
}
}
- Return to the form and click the list view
- In the Properties window, click Events and double-click
ItemSelectionChanged
- 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);
}
}
- To execute, press F5
- Click the New Real Estate Property button and
create a property
- Click OK
- 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):
- Create the following properties
- Close the form and return to your programming environment
Practical Learning: Checking a Node
|
|
- On the form, click the list view
- In the Events section of the Properties window, double-click
DoubleClick
- 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(stmProperties);
// 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();
}
- Press F5 to execute the application
- Double-click one of the properties
- Close the forms and return to your programming environment
Practical Learning: Updating a Node
|
|
- 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();
}
}
- Press F5 to execute
- Double-click the row in the Townhouse section
- Change its year built to 2005
- Change its market value to 585985 (If possible,
also change its picture)
- Click OK
- Close the form and return to your programming environment
Practical Learning: Navigating Among Nodes
|
|
- To create a new form, on the main menu, click Projects -> Add
Windows Form...
- Set the Name to PropertiesReview
- Click Add
- 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 |
|
|
|
PictureBox |
|
pbxProperty |
|
|
BorderStyle: FixedSingle SizeMode: Zoom |
Button |
|
btnClose |
|
Close |
|
Button |
|
btnFirst |
|
First |
|
Label |
|
lblRecordNumber |
|
000 of 000 |
|
Button |
|
btnPrevious |
|
Previous |
|
Button |
|
btnLast |
|
Last |
|
|
- Double-click an unoccupied area of the form
- 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(stmProperties);
btnFirst_Click(sender, e);
}
}
}
}
- Return to the form and double-click the First button
- 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();
}
- Return to the form and double-click the Previous button
- 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();
}
}
- Return to the form and double-click the Next button
- 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();
}
}
- Return to the form and double-click the Last button
- 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();
}
- Return to the form and double-click the Close button
- Type Close();
- Display the Properties Listing form
- 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...
- 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();
}
- Press F5 to execute
- Click the Review Properties button
- Close the forms and return to your programming environment
Practical Learning: Inserting Nodes
|
|
- Display the Properties Listing form
- In the Menus & Toolbars section of the Toolbox, click
ContextMenuStrip and click the form
- Click the menu items as follows:
(Name) |
Enabled |
Text |
mnuNewProperty |
|
New Property... |
mnuEditProperty |
False |
Edit Property... |
mnuInsertBefore |
False |
Insert Property... |
mnuInsertAfter |
False |
Insert After |
- Under the form, click the context menu strip
- In the Properties window, change its name to cmsProperties
- On the form, click the list view
- In the Properties window, set its ContextMenuStrip to cmsProperties
- Under the form, click cmsProperties
- Under ContextMenuStrip, double-click New Property...
- Implement the event as follows:
private void mnuNewProperty_Click(object sender, EventArgs e)
{
btnNewProperty_Click(sender, e);
}
- 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;
}
}
- Return to the form and, under the form, click cmsProperties
- On the form, double-click Edit properties
- Implement its event as follows:
private void mnuEditProperty_Click(object sender, EventArgs e)
{
lvwProperties_DoubleClick(sender, e);
}
- Return to the form and, on the form, double-click Insert Before...
- 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();
}
}
}
- Return to the form and, on the form, double-click Insert After...
- 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(stmProperties);
// 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();
}
}
}
- To execute, press F5
- Create the properties (let the application generate the
property number) (the new records are in bold):
- Close the form and return to your programming environment
Practical Learning: Deleting a Node
|
|
- Display the Properties Listing form
- Under the form, click cmsProperties
- 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 |
- Double-click the Delete Property menu item
- 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;
}
}
- 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(stmProperties);
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();
}
}
}
- Return to the form and double-click the Close button
- Implement it as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- To execute, press F5
- Right-click one of the records and click Delete Property
- Click No
- Right-click another row and click Delete Property
- Click Yes
- Close the form and return to your programming environment
|
|