You are on page 1of 32

Collection-Based Application: Altair Realtors

Source: http://www.csharpkey.com/visualcsharp/collections/ar.htm

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:
10. using System;
11. using System.Collections.Generic;
12. using System.Linq;
13. using System.Text;
14.
15. namespace AltairRealtors1
16. {
17. [Serializable]
18. public class RealEstateProperty
19. {
20. public string PropertyNumber { get; set; }
21. public string PropertyType { get; set; }
22. public string Address { get; set; }
23. public string City { get; set; }
24. public string State { get; set; }
25. public string ZIPCode { get; set; }
26. public short Stories { get; set; }
27. public int YearBuilt { get; set; }
28. public short Bedrooms { get; set; }
29. public float Bathrooms { get; set; }
30. public string Condition { get; set; }
31. public string SaleStatus { get; set; }
32. public double MarketValue { get; set; }
33. public string PictureFile { get; set; }
34.
35. // To determine that two properties are the same,
36. // we will test only the property number.
37. // We assume that if two properties have the same number,
38. // then it is the same property
39. public override bool Equals(object obj)
40. {
41. RealEstateProperty rep = (RealEstateProperty)obj;
42.
43. if (rep.PropertyNumber == PropertyNumber)
44. return true;
45. else
46. return false;
47. }
48.
49. // To avoid a compiler warning
50. public override int GetHashCode()
51. {
52. return base.GetHashCode();
53. }
54. }
}
55. In the Solution Explorer, right-click Form1.cs and click Rename
56. Type AltairRealtors.cs and press Enter twice to display the form
57. From the Toolbox, add a list view to the form
58. Right-click that list box on the click Edit Columns...
59. 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

60. Click OK
61. 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
62. Right-click the form and click Edit Groups...
63. Create the groups as follows:

Header Name
Condominium lvgCondominium
Townhouse lvgTownhouse
Single Family lvgSingleFamily

64. Click OK
65. To create a new form, on the main menu, click Projects -> Add Windows Form...
66. Set the Name to PropertyEditor
67. Click Add
68. In the Dialogs section of the Toolbox, click OpenFileDialog
69. Click the form
70. In the Properties window, change its characteristics as follows:
(Name): dlgPicture
DefaultExt: jpg
Filter: JPEG Files (*.jpg,*.jpeg)|*.jpg
Title: Select House Picture
71. Design the form as follows:
Other
Control (Name) DropDownStyle Text Items Modifiers
Properties
Property
Label
Type:
Condominium
Townhouse
ComboBox cbxPropertyTypes DropDownList Public
Single Family
Unknown

Label Property #:

TextBox txtPropertyNumber Public

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,
ComboBox cbxStates DropDownList MS, MO, MT, Public
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
ComboBox cbxConditions DropDownList Good Shape Public
Needs Fixing

Label Bedrooms:

TextBox txtBedrooms 0 Public

Label Bathrooms:

TextBox txtBathrooms 0.00 Public


Market
Label
Value:

TextBox txtMarketValue 0.00 Public

Sale
Label
Status:
Unspecified
ComboBox cbxSaleStatus DropDownList Available Public
Sold

Button btnPicture Picture...

BorderSty
FixedSing
PictureBox pbxProperty
SizeMode
Zoom
DialogRes
Button btnOK OK
OK
DialogRes
Button btnCancel Cancel
Cancel
Form
FormBorderStyle: FixedDialog
Text: Altair Realtors - Property Editor
StartPosition: CenterScreen
AcceptButton: btnOK
CancelButton: btnCancel
MaximizeBox: False
MinimizeBox: False
ShowInTaskBar: False
72. Double-click an unoccupied area of the form
73. Return to the form
74. Double-click the Picture button
75. Change the file as follows:
76. using System;
77. using System.Collections.Generic;
78. using System.ComponentModel;
79. using System.Data;
80. using System.Drawing;
81. using System.Linq;
82. using System.Text;
83. using System.Windows.Forms;
84.
85. namespace AltairRealtors1
86. {
87. public partial class PropertyEditor : Form
88. {
89. public bool pictureChanged;
90. public string pictureFile;
91.
92. public PropertyEditor()
93. {
94. InitializeComponent();
95. }
96.
97. private void PropertyEditor_Load(object sender, EventArgs e)
98. {
99. pictureChanged = false;
100. pictureFile = "C:\\Altair Realtors1\\000-000.jpg";
101. }
102.
103. private void btnPicture_Click(object sender, EventArgs e)
104. {
105. if (dlgPicture.ShowDialog() == DialogResult.OK)
106. {
107. pbxProperty.Image =
Image.FromFile(dlgPicture.FileName);
108. pictureFile = dlgPicture.FileName;
109. pictureChanged = true;
110. }
111. }
112. }
}
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:
4. using System;
5. using System.Collections.Generic;
6. using System.ComponentModel;
7. using System.Data;
8. using System.Drawing;
9. using System.Linq;
10. using System.Text;
11. using System.Windows.Forms;
12. using System.IO;
13. using System.Runtime.Serialization.Formatters.Binary;
14.
15. namespace AltairRealtors10
16. {
17. public partial class AltairRealtors : Form
18. {
19. LinkedList<RealEstateProperty> properties;
20.
21. public AltairRealtors()
22. {
23. InitializeComponent();
24. }
25.
26. private void ShowProperties()
27. {
28. // Get a reference to the file that holds the records of
properties
29. string Filename = "C:\\Altair Realtors1\\properties.atr";
30.
31. // Make sure the file exists
32. if (File.Exists(Filename) == true)
33. {
34. // if so, create a file stream
35. FileStream stmProperties = new FileStream(Filename,
36.
FileMode.Open,
37.
FileAccess.Read);
38. // Create a binary formatter
39. BinaryFormatter bfmProperty = new BinaryFormatter();
40. // If some properties were created already,
41. // get them and store them in the collection
42. properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmProperties);
43.
44. // First, empty the list view
45. lvwProperties.Items.Clear();
46. ListViewItem lviProperty = null;
47.
48. // Visit each property in the collection and add it to
the list view
49. foreach (RealEstateProperty house in properties)
50. {
51. if (house.PropertyType.Equals("Condominium"))
52. lviProperty = new
ListViewItem(house.PropertyNumber, lvwProperties.Groups[0]);
53. else if (house.PropertyType.Equals("Townhouse"))
54. lviProperty = new
ListViewItem(house.PropertyNumber, lvwProperties.Groups[1]);
55. else // if (house.PropertyType.Equals("Single
Family"))
56. lviProperty = new
ListViewItem(house.PropertyNumber, lvwProperties.Groups[2]);
57.
58. lviProperty.SubItems.Add(house.City);
59.
lviProperty.SubItems.Add(house.Stories.ToString());
60.
lviProperty.SubItems.Add(house.YearBuilt.ToString());
61.
lviProperty.SubItems.Add(house.Bedrooms.ToString());
62.
lviProperty.SubItems.Add(house.Bathrooms.ToString("F"));
63. lviProperty.SubItems.Add(house.Condition);
64. lviProperty.SubItems.Add(house.SaleStatus);
65.
lviProperty.SubItems.Add(house.MarketValue.ToString());
66. lvwProperties.Items.Add(lviProperty);
67. }
68.
69. // Close the file stream
70. stmProperties.Close();
71. }
72. }
73.
74. private void AltairRealtors_Load(object sender, EventArgs e)
75. {
76. properties = new LinkedList<RealEstateProperty>();
77. ShowProperties();
78. }
79. }
}
80.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:
3. private void btnNewProperty_Click(object sender, EventArgs e)
4. {
5. PropertyEditor editor = new PropertyEditor();
6.
7. Random rndNumber = new Random(DateTime.Now.Millisecond);
8. int number1 = rndNumber.Next(100, 999);
9. int number2 = rndNumber.Next(100, 999);
10. string propNumber = number1 + "-" + number2;
11.
12. editor.txtPropertyNumber.Text = propNumber;
13.
14. // Check that the directory that contains the list of properties
exists.
15. // If it doesn't exist, create it
16. DirectoryInfo dirInfo = Directory.CreateDirectory("C:\\Altair
Realtors1");
17. // Get a reference to the file that holds the properties
18. string Filename = "C:\\Altair Realtors1\\properties.atr";
19.
20. // First check if the file was previously created
21. if (File.Exists(Filename) == true)
22. {
23. // If the list of properties exists already,
24. // get it and store it in a file stream
25. FileStream stmProperties = new FileStream(Filename,
26. FileMode.Open,
27. FileAccess.Read);
28. BinaryFormatter bfmProperty = new BinaryFormatter();
29. // Store the list of properties in the collection
30. properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmProperties);
31. // Close the file stream
32. stmProperties.Close();
33. }
34.
35. if (editor.ShowDialog() == DialogResult.OK)
36. {
37. RealEstateProperty prop = new RealEstateProperty();
38.
39. prop.PropertyNumber = editor.txtPropertyNumber.Text;
40. prop.PropertyType = editor.cbxPropertyTypes.Text;
41. prop.Address = editor.txtAddress.Text;
42. prop.City = editor.txtCity.Text;
43. prop.State = editor.cbxStates.Text;
44. prop.ZIPCode = editor.txtZIPCode.Text;
45. prop.Stories = short.Parse(editor.txtStories.Text);
46. prop.YearBuilt = int.Parse(editor.txtYearBuilt.Text);
47. prop.Bedrooms = short.Parse(editor.txtBedrooms.Text);
48. prop.Bathrooms = float.Parse(editor.txtBathrooms.Text);
49. prop.Condition = editor.cbxConditions.Text;
50. prop.SaleStatus = editor.cbxSaleStatus.Text;
51. prop.MarketValue = double.Parse(editor.txtMarketValue.Text);
52. if (!editor.pictureFile.Equals(""))
53. {
54. FileInfo flePicture = new FileInfo(editor.pictureFile);
55. flePicture.CopyTo("C:\\Altair Realtors1\\" +
56. editor.txtPropertyNumber.Text +
57. flePicture.Extension);
58. prop.PictureFile = "C:\\Altair Realtors1\\" +
59. editor.txtPropertyNumber.Text +
60. flePicture.Extension;
61. }
62. else
63. prop.PictureFile = "C:\\Altair Realtors1\\000-000.jpg";
64. // Add the property in the collection
65. properties.AddFirst(prop);
66.
67. // Get a reference to the properties file
68. string strFilename = dirInfo.FullName + "\\properties.atr";
69. // Create a file stream to hold the list of properties
70. FileStream stmProperties = new FileStream(strFilename,
71. FileMode.Create,
72. FileAccess.Write);
73. BinaryFormatter bfmProperty = new BinaryFormatter();
74.
75. // Serialize the list of properties
76. bfmProperty.Serialize(stmProperties, properties);
77. // Close the file stream
78. stmProperties.Close();
79.
80. // Show the list of properties
81. ShowProperties();
82. }
}
83. Return to the form and click the list view
84. In the Properties window, click Events and double-click ItemSelectionChanged
85. Implement the event as follows:
86. private void lvwProperties_ItemSelectionChanged(object sender,
87. ListViewItemSelectionChangedEventArgs e)
88. {
89. RealEstateProperty currentProperty = new RealEstateProperty();
90.
91. foreach (RealEstateProperty prop in properties)
92. {
93. if (prop.PropertyNumber.Equals(e.Item.SubItems[0].Text))
94. pbxProperty.Image = Image.FromFile(prop.PictureFile);
95. }
}
96. To execute, press F5
97. Click the New Real Estate Property button and create a property

98. Click OK
99. 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):
100. Create the following properties

101. Close the form and return to your programming environment


Topic Applied: Checking a Node
1. On the form, click the list view
2. In the Events section of the Properties window, double-click DoubleClick
3. Implement its event as follows:
4. private void lvwProperties_DoubleClick(object sender, EventArgs e)
5. {
6. if ((lvwProperties.SelectedItems.Count == 0) ||
7. (lvwProperties.SelectedItems.Count > 1))
8. return;
9.
10. // Get a reference to the file that holds the properties
11. string Filename = "C:\\Altair Realtors1\\properties.atr";
12.
13. // Open the file that contains the properties
14. FileStream stmProperties = new FileStream(Filename,
15. FileMode.Open,
16. FileAccess.Read);
17. BinaryFormatter bfmProperty = new BinaryFormatter();
18. // Store the list of properties in the collection
19. properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmProperties);
20. // Close the file stream
21. stmProperties.Close();
22.
23. // Get the property that the user double-clicked
24. var lviProperty = lvwProperties.SelectedItems[0];
25. RealEstateProperty house = new RealEstateProperty();
26. foreach( RealEstateProperty prop in properties )
27. if( prop.PropertyNumber == lviProperty.Text )
28. house = prop;
29.
30. // Get a reference to the property editor
31. PropertyEditor editor = new PropertyEditor();
32.
33. // Prepare to fill the editor with the values of the property the
user double-clicked
34. editor.txtPropertyNumber.Text = house.PropertyNumber;
35. editor.cbxPropertyTypes.Text = house.PropertyType;
36. editor.txtAddress.Text = house.Address;
37. editor.txtCity.Text = house.City;
38. editor.cbxStates.Text = house.State;
39. editor.txtZIPCode.Text = house.ZIPCode;
40. editor.txtStories.Text = house.Stories.ToString();
41. editor.txtYearBuilt.Text = house.YearBuilt.ToString();
42. editor.txtBedrooms.Text = house.Bedrooms.ToString();
43. editor.txtBathrooms.Text = house.Bathrooms.ToString("F");
44. editor.cbxConditions.Text = house.Condition;
45. editor.cbxSaleStatus.Text = house.SaleStatus;
46. editor.txtMarketValue.Text = house.MarketValue.ToString("F");
47. editor.pbxProperty.Image = Image.FromFile(house.PictureFile);
48.
49. // Disable the property number just in case the user tries to
change it
50. editor.txtPropertyNumber.Enabled = false;
51.
52. // Show the property editor
53. editor.ShowDialog();
}
54. Press F5 to execute the application
55. Double-click one of the properties
56. Close the forms and return to your programming environment
Topic Applied: Updating a Node
1. Change the DoubleClick event as follows:
2. private void lvwProperties_DoubleClick(object sender, EventArgs e)
3. {
4. if ((lvwProperties.SelectedItems.Count == 0) ||
5. (lvwProperties.SelectedItems.Count > 1))
6. return;
7.
8. // Get a reference to the file that holds the properties
9. string Filename = "C:\\Altair Realtors1\\properties.atr";
10.
11. // Open the file that contains the properties
12. FileStream stmProperties = new FileStream(Filename,
13. FileMode.Open,
14. FileAccess.Read);
15. BinaryFormatter bfmProperty = new BinaryFormatter();
16. // Store the list of properties in the linked list
17. properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmProperties);
18. // Close the file stream
19. stmProperties.Close();
20.
21. // Get the property that the user double-clicked
22. var lviProperty = lvwProperties.SelectedItems[0];
23. RealEstateProperty house = new RealEstateProperty();
24. // Get the property number the user double-clicked
25. house.PropertyNumber = lviProperty.Text;
26.
27. // Find the property in the list
28. LinkedListNode<RealEstateProperty> nodProperty =
properties.Find(house);
29.
30. // Get a reference to the property editor
31. PropertyEditor editor = new PropertyEditor();
32.
33. // Prepare to fill the editor with the values of the property the
user double-clicked
34. editor.txtPropertyNumber.Text = nodProperty.Value.PropertyNumber;
35. editor.cbxPropertyTypes.Text = nodProperty.Value.PropertyType;
36. editor.txtAddress.Text = nodProperty.Value.Address;
37. editor.txtCity.Text = nodProperty.Value.City;
38. editor.cbxStates.Text = nodProperty.Value.State;
39. editor.txtZIPCode.Text = nodProperty.Value.ZIPCode;
40. editor.txtStories.Text = nodProperty.Value.Stories.ToString();
41. editor.txtYearBuilt.Text = nodProperty.Value.YearBuilt.ToString();
42. editor.txtBedrooms.Text = nodProperty.Value.Bedrooms.ToString();
43. editor.txtBathrooms.Text =
nodProperty.Value.Bathrooms.ToString("F");
44. editor.cbxConditions.Text = nodProperty.Value.Condition;
45. editor.cbxSaleStatus.Text = nodProperty.Value.SaleStatus;
46. editor.txtMarketValue.Text =
nodProperty.Value.MarketValue.ToString("F");
47. editor.pbxProperty.Image =
Image.FromFile(nodProperty.Value.PictureFile);
48. editor.pictureFile = nodProperty.Value.PictureFile;
49.
50. // Disable the property number so the user cannot change it
51. editor.txtPropertyNumber.Enabled = false;
52.
53. // Show the property editor
54. if (editor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
55. {
56. // For each value that has changed in the property, update its
nnode
57. nodProperty.Value.PropertyType = editor.cbxPropertyTypes.Text;
58. nodProperty.Value.Address = editor.txtAddress.Text;
59. nodProperty.Value.City = editor.txtCity.Text;
60. nodProperty.Value.State = editor.cbxStates.Text;
61. nodProperty.Value.ZIPCode = editor.txtZIPCode.Text;
62. nodProperty.Value.Stories =
short.Parse(editor.txtStories.Text);
63. nodProperty.Value.YearBuilt =
int.Parse(editor.txtYearBuilt.Text);
64. nodProperty.Value.Bedrooms =
short.Parse(editor.txtBedrooms.Text);
65. nodProperty.Value.Bathrooms =
float.Parse(editor.txtBathrooms.Text);
66. nodProperty.Value.Condition = editor.cbxConditions.Text;
67. nodProperty.Value.SaleStatus = editor.cbxSaleStatus.Text;
68. nodProperty.Value.MarketValue =
double.Parse(editor.txtMarketValue.Text);
69. nodProperty.Value.PictureFile = editor.pictureFile;
70.
71. // Start saving the linked list
72. stmProperties = new FileStream(Filename,
73. FileMode.OpenOrCreate,
74. FileAccess.ReadWrite);
75. bfmProperty = new BinaryFormatter();
76.
77. // Serialize the list of properties
78. bfmProperty.Serialize(stmProperties, properties);
79. // Close the file stream
80. stmProperties.Close();
81.
82. // Show the list of properties
83. ShowProperties();
84. }
}
85. Press F5 to execute
86. Double-click the row in the Townhouse section
87. Change its year built to 2005
88.Change its market value to 585985 (If possible, also change its picture)
89. Click OK

90. 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

Property
Label
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

Market
Label
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:
7. using System;
8. using System.Collections.Generic;
9. using System.ComponentModel;
10. using System.Data;
11. using System.Drawing;
12. using System.Linq;
13. using System.Text;
14. using System.Windows.Forms;
15. using System.IO;
16. using System.Runtime.Serialization.Formatters.Binary;
17.
18. namespace AltairRealtors10
19. {
20. public partial class PropertiesReview : Form
21. {
22. int index;
23. LinkedList<RealEstateProperty> properties;
24. LinkedListNode<RealEstateProperty> nodCurrent;
25.
26. public PropertiesReview()
27. {
28. InitializeComponent();
29. }
30.
31. private void PropertiesReview_Load(object sender, EventArgs e)
32. {
33. index = 1;
34. // Get a reference to the file that holds the records of
properties
35. string Filename = "C:\\Altair Realtors1\\properties.atr";
36.
37. // Make sure the file exists
38. if (File.Exists(Filename) == true)
39. {
40. // if so, create a file stream
41. FileStream stmProperties = new FileStream(Filename,
42.
FileMode.Open,
43.
FileAccess.Read);
44. // Create a binary formatter
45. BinaryFormatter bfmProperty = new BinaryFormatter();
46. // If some properties were created already,
47. // get them and store them in the collection
48. properties =
(LinkedList<RealEstateProperty>)bfmProperty.Deserialize(stmProperties);
49.
50. btnFirst_Click(sender, e);
51. }
52. }
53. }
}
54. Return to the form and double-click the First button
55. Implement its event as follows:
56. private void btnFirst_Click(object sender, EventArgs e)
57. {
58. index = 1;
59. nodCurrent = properties.First;
60.
61. txtPropertyNumber.Text = nodCurrent.Value.PropertyNumber;
62. txtPropertyType.Text = nodCurrent.Value.PropertyType;
63. txtAddress.Text = nodCurrent.Value.Address;
64. txtCity.Text = nodCurrent.Value.City;
65. txtState.Text = nodCurrent.Value.State;
66. txtZIPCode.Text = nodCurrent.Value.ZIPCode;
67. txtStories.Text = nodCurrent.Value.Stories.ToString();
68. txtYearBuilt.Text = nodCurrent.Value.YearBuilt.ToString();
69. txtBedrooms.Text = nodCurrent.Value.Bedrooms.ToString();
70. txtBathrooms.Text = nodCurrent.Value.Bathrooms.ToString("F");
71. txtCondition.Text = nodCurrent.Value.Condition;
72. txtSaleStatus.Text = nodCurrent.Value.SaleStatus;
73. txtMarketValue.Text = nodCurrent.Value.MarketValue.ToString("F");
74. pbxProperty.Image = Image.FromFile(nodCurrent.Value.PictureFile);
75.
76. lblRecordNumber.Text = "1 of " + properties.Count.ToString();
}
77. Return to the form and double-click the Previous button
78. Implement its event as follows:
79. private void btnPrevious_Click(object sender, EventArgs e)
80. {
81. index--;
82.
83. if (index <= 1)
84. btnFirst_Click(sender, e);
85. else
86. {
87. LinkedListNode<RealEstateProperty> current =
nodCurrent.Previous;
88.
89. txtPropertyNumber.Text = current.Value.PropertyNumber;
90. txtPropertyType.Text = current.Value.PropertyType;
91. txtAddress.Text = current.Value.Address;
92. txtCity.Text = current.Value.City;
93. txtState.Text = current.Value.State;
94. txtZIPCode.Text = current.Value.ZIPCode;
95. txtStories.Text = current.Value.Stories.ToString();
96. txtYearBuilt.Text = current.Value.YearBuilt.ToString();
97. txtBedrooms.Text = current.Value.Bedrooms.ToString();
98. txtBathrooms.Text = current.Value.Bathrooms.ToString("F");
99. txtCondition.Text = current.Value.Condition;
100. txtSaleStatus.Text = current.Value.SaleStatus;
101. txtMarketValue.Text = current.Value.MarketValue.ToString("F");
102.
103. lblRecordNumber.Text = index.ToString() +
104. " of " + properties.Count.ToString();
105. }
}
106. Return to the form and double-click the Next button
107. Implement its event as follows:
108. private void btnNext_Click(object sender, EventArgs e)
109. {
110. index++;
111.
112. if (index >= properties.Count)
113. btnLast_Click(sender, e);
114. else
115. {
116. LinkedListNode<RealEstateProperty> current = nodCurrent.Next;
117.
118. txtPropertyNumber.Text = current.Value.PropertyNumber;
119. txtPropertyType.Text = current.Value.PropertyType;
120. txtAddress.Text = current.Value.Address;
121. txtCity.Text = current.Value.City;
122. txtState.Text = current.Value.State;
123. txtZIPCode.Text = current.Value.ZIPCode;
124. txtStories.Text = current.Value.Stories.ToString();
125. txtYearBuilt.Text = current.Value.YearBuilt.ToString();
126. txtBedrooms.Text = current.Value.Bedrooms.ToString();
127. txtBathrooms.Text = current.Value.Bathrooms.ToString("F");
128. txtCondition.Text = current.Value.Condition;
129. txtSaleStatus.Text = current.Value.SaleStatus;
130. txtMarketValue.Text = current.Value.MarketValue.ToString("F");
131. pbxProperty.Image = Image.FromFile(current.Value.PictureFile);
132.
133. lblRecordNumber.Text = index.ToString() +
134. " of " + properties.Count.ToString();
135. }
}
136. Return to the form and double-click the Last button
137. Implement its event as follows:
138. private void btnLast_Click(object sender, EventArgs e)
139. {
140. index = properties.Count;
141. nodCurrent = properties.Last;
142.
143. txtPropertyNumber.Text = nodCurrent.Value.PropertyNumber;
144. txtPropertyType.Text = nodCurrent.Value.PropertyType;
145. txtAddress.Text = nodCurrent.Value.Address;
146. txtCity.Text = nodCurrent.Value.City;
147. txtState.Text = nodCurrent.Value.State;
148. txtZIPCode.Text = nodCurrent.Value.ZIPCode;
149. txtStories.Text = nodCurrent.Value.Stories.ToString();
150. txtYearBuilt.Text = nodCurrent.Value.YearBuilt.ToString();
151. txtBedrooms.Text = nodCurrent.Value.Bedrooms.ToString();
152. txtBathrooms.Text = nodCurrent.Value.Bathrooms.ToString("F");
153. txtCondition.Text = nodCurrent.Value.Condition;
154. txtSaleStatus.Text = nodCurrent.Value.SaleStatus;
155. txtMarketValue.Text = nodCurrent.Value.MarketValue.ToString("F");
156. pbxProperty.Image = Image.FromFile(nodCurrent.Value.PictureFile);
157.
158. lblRecordNumber.Text = properties.Count.ToString() +
159. " of " + properties.Count.ToString();
}
160. Return to the form and double-click the Close button
161. Type Close();
162. Display the Properties Listing form
163. 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...
164. Double-click the Navigate button and implement its event as follows:
165. private void btnReviewProperties_Click(object sender, EventArgs e)
166. {
167. PropertiesReview pr = new PropertiesReview();
168.
169. pr.ShowDialog();
}
170. Press F5 to execute
171. Click the Review Properties button
172. 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:
11. private void mnuNewProperty_Click(object sender, EventArgs e)
12. {
13. btnNewProperty_Click(sender, e);
}
14. Locate the ItemSelectionChanged event and change its implementation as
follows:
15. private void lvwProperties_ItemSelectionChanged(object sender,
16. ListViewItemSelectionChangedEventArgs e)
17. {
18. RealEstateProperty currentProperty = new RealEstateProperty();
19.
20. if (lvwProperties.SelectedItems.Count == 1)
21. lviSelected = lvwProperties.SelectedItems[0];
22.
23. foreach (RealEstateProperty prop in properties)
24. {
25. if (prop.PropertyNumber.Equals(e.Item.SubItems[0].Text))
26. pbxProperty.Image = Image.FromFile(prop.PictureFile);
27. }
28.
29. if( lvwProperties.SelectedItems.Count == 1)
30. {
31. mnuInsertBefore.Enabled = true;
32. mnuEditProperty.Enabled = true;
33. mnuInsertAfter.Enabled = true;
34. }
35. else
36. {
37. mnuInsertBefore.Enabled = false;
38. mnuEditProperty.Enabled = false;
39. mnuInsertAfter.Enabled = false;
40. }
}
41. Return to the form and, under the form, click cmsProperties
42. On the form, double-click Edit properties
43. Implement its event as follows:
44. private void mnuEditProperty_Click(object sender, EventArgs e)
45. {
46. lvwProperties_DoubleClick(sender, e);
}
47. Return to the form and, on the form, double-click Insert Before...
48. Implement the event as follows:
49. private void mnuInsertBefore_Click(object sender, EventArgs e)
50. {
51. PropertyEditor editor = new PropertyEditor();
52. string Filename = "C:\\Altair Realtors1\\properties.atr";
53. DirectoryInfo dirInfo = Directory.CreateDirectory("C:\\Altair
Realtors1");
54.
55. if (File.Exists(Filename) == true)
56. {
57. FileStream stmProperties = new FileStream(Filename,
58. FileMode.Open,
59. FileAccess.Read);
60. BinaryFormatter bfmProperties = new BinaryFormatter();
61.
62. properties =
(LinkedList<RealEstateProperty>)bfmProperties.Deserialize(stmProperties
);
63. stmProperties.Close();
64. stmProperties.Close();
65. }
66.
67. RealEstateProperty rep = new RealEstateProperty();
68. string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
69. rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;
70.
71. Random rndNumber = new Random(DateTime.Now.Millisecond);
72. int number1 = rndNumber.Next(100, 999);
73. int number2 = rndNumber.Next(100, 999);
74. string propNumber = number1 + "-" + number2;
75.
76. editor.txtPropertyNumber.Text = propNumber;
77. editor.cbxPropertyTypes.Text =
lvwProperties.SelectedItems[0].Group.Header;
78. editor.cbxPropertyTypes.Enabled = false;
79.
80. LinkedListNode<RealEstateProperty> nodProperty =
properties.Find(rep);
81.
82. if (nodProperty != null)
83. {
84. if (editor.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
85. {
86. RealEstateProperty prop = new RealEstateProperty();
87.
88. prop.PropertyNumber = editor.txtPropertyNumber.Text;
89. prop.PropertyType = editor.cbxPropertyTypes.Text;
90. prop.Address = editor.txtAddress.Text;
91. prop.City = editor.txtCity.Text;
92. prop.State = editor.cbxStates.Text;
93. prop.ZIPCode = editor.txtZIPCode.Text;
94. prop.Stories = short.Parse(editor.txtStories.Text);
95. prop.YearBuilt = int.Parse(editor.txtYearBuilt.Text);
96. prop.Bedrooms = short.Parse(editor.txtBedrooms.Text);
97. prop.Bathrooms = float.Parse(editor.txtBathrooms.Text);
98. prop.Condition = editor.cbxConditions.Text;
99. prop.SaleStatus = editor.cbxSaleStatus.Text;
100. prop.MarketValue =
double.Parse(editor.txtMarketValue.Text);
101. if (!editor.pictureFile.Equals(""))
102. {
103. FileInfo flePicture = new
FileInfo(editor.pictureFile);
104. flePicture.CopyTo("C:\\Altair Realtors1\\" +
105. editor.txtPropertyNumber.Text +
106. flePicture.Extension);
107. prop.PictureFile = "C:\\Altair Realtors1\\" +
108. editor.txtPropertyNumber.Text +
109. flePicture.Extension;
110. }
111. else
112. prop.PictureFile = "C:\\Altair Realtors1\\000-
000.jpg";
113.
114. // Insert the property before the currently selected node
115. properties.AddBefore(nodProperty, prop);
116.
117. // Get a reference to the properties file
118. string strFilename = dirInfo.FullName +
"\\properties.atr";
119. // Create a file stream to hold the list of properties
120. FileStream stmProperties = new FileStream(strFilename,
121. FileMode.Create,
122.
FileAccess.Write);
123. BinaryFormatter bfmProperty = new BinaryFormatter();
124.
125. // Serialize the list of properties
126. bfmProperty.Serialize(stmProperties, properties);
127. // Close the file stream
128. stmProperties.Close();
129.
130. // Show the list of properties
131. ShowProperties();
132. }
133. }
}
134. Return to the form and, on the form, double-click Insert After...
135. Implement the event as follows:
136. private void mnuInsertAfter_Click(object sender, EventArgs e)
137. {
138. PropertyEditor editor = new PropertyEditor();
139. string Filename = "C:\\Altair Realtors1\\properties.atr";
140. DirectoryInfo dirInfo = Directory.CreateDirectory("C:\\Altair
Realtors1");
141.
142. if (File.Exists(Filename) == true)
143. {
144. FileStream stmProperties = new FileStream(Filename,
145. FileMode.Open,
146. FileAccess.Read);
147. BinaryFormatter bfmProperties = new BinaryFormatter();
148.
149. properties =
(LinkedList<RealEstateProperty>)bfmProperties.Deserialize(stmProperties
);
150. // Close the file stream
151. stmProperties.Close();
152. stmProperties.Close();
153. }
154.
155. RealEstateProperty rep = new RealEstateProperty();
156. string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
157. rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;
158.
159. Random rndNumber = new Random(DateTime.Now.Millisecond);
160. int number1 = rndNumber.Next(100, 999);
161. int number2 = rndNumber.Next(100, 999);
162. string propNumber = number1 + "-" + number2;
163.
164. editor.txtPropertyNumber.Text = propNumber;
165. editor.cbxPropertyTypes.Text =
lvwProperties.SelectedItems[0].Group.Header;
166. editor.cbxPropertyTypes.Enabled = false;
167.
168. LinkedListNode<RealEstateProperty> nodProperty =
properties.Find(rep);
169.
170. if (nodProperty != null)
171. {
172. if (editor.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
173. {
174. RealEstateProperty prop = new RealEstateProperty();
175.
176. prop.PropertyNumber = editor.txtPropertyNumber.Text;
177. prop.PropertyType = editor.cbxPropertyTypes.Text;
178. prop.Address = editor.txtAddress.Text;
179. prop.City = editor.txtCity.Text;
180. prop.State = editor.cbxStates.Text;
181. prop.ZIPCode = editor.txtZIPCode.Text;
182. prop.Stories = short.Parse(editor.txtStories.Text);
183. prop.YearBuilt = int.Parse(editor.txtYearBuilt.Text);
184. prop.Bedrooms = short.Parse(editor.txtBedrooms.Text);
185. prop.Bathrooms = float.Parse(editor.txtBathrooms.Text);
186. prop.Condition = editor.cbxConditions.Text;
187. prop.SaleStatus = editor.cbxSaleStatus.Text;
188. prop.MarketValue =
double.Parse(editor.txtMarketValue.Text);
189. if (!editor.pictureFile.Equals(""))
190. {
191. FileInfo flePicture = new
FileInfo(editor.pictureFile);
192. flePicture.CopyTo("C:\\Altair Realtors1\\" +
193. editor.txtPropertyNumber.Text +
194. flePicture.Extension);
195. prop.PictureFile = "C:\\Altair Realtors1\\" +
196. editor.txtPropertyNumber.Text +
197. flePicture.Extension;
198. }
199. else
200. prop.PictureFile = "C:\\Altair Realtors1\\000-
000.jpg";
201.
202. // Insert the property before the currently selected node
203. properties.AddAfter(nodProperty, prop);
204.
205. // Get a reference to the properties file
206. string strFilename = dirInfo.FullName +
"\\properties.atr";
207. // Create a file stream to hold the list of properties
208. FileStream stmProperties = new FileStream(strFilename,
209. FileMode.Create,
210.
FileAccess.Write);
211. BinaryFormatter bfmProperty = new BinaryFormatter();
212.
213. // Serialize the list of properties
214. bfmProperty.Serialize(stmProperties, properties);
215. // Close the file stream
216. stmProperties.Close();
217.
218. // Show the list of properties
219. ShowProperties();
220. }
221. }
}
222. To execute, press F5
223. Create the properties (let the application generate the property number)
(the new records are in bold):

224. 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:
6. private void lvwProperties_ItemSelectionChanged(object sender,
7. ListViewItemSelectionChangedEventArgs e)
8. {
9. RealEstateProperty currentProperty = new RealEstateProperty();
10.
11. if (lvwProperties.SelectedItems.Count == 1)
12. lviSelected = lvwProperties.SelectedItems[0];
13.
14. foreach (RealEstateProperty prop in properties)
15. {
16. if (prop.PropertyNumber.Equals(e.Item.SubItems[0].Text))
17. pbxProperty.Image = Image.FromFile(prop.PictureFile);
18. }
19.
20. if( lvwProperties.SelectedItems.Count == 1)
21. {
22. mnuInsertBefore.Enabled = true;
23. mnuEditProperty.Enabled = true;
24. mnuInsertAfter.Enabled = true;
25. mnuDeleteProperty.Enabled = true;
26. }
27. else
28. {
29. mnuInsertBefore.Enabled = false;
30. mnuEditProperty.Enabled = false;
31. mnuInsertAfter.Enabled = false;
32. mnuDeleteProperty.Enabled = false;
33. }
}
34. Scroll down and implement the new event as follows:
35. private void mnuDeleteProeprty_Click(object sender, EventArgs e)
36. {
37. PropertyEditor editor = new PropertyEditor();
38. string Filename = "C:\\Altair Realtors1\\properties.atr";
39. DirectoryInfo dirInfo = Directory.CreateDirectory("C:\\Altair
Realtors1");
40.
41. // Open the file that holds the properties
42. if (File.Exists(Filename) == true)
43. {
44. FileStream stmProperties = new FileStream(Filename,
45. FileMode.Open,
46. FileAccess.Read);
47. BinaryFormatter bfmProperties = new BinaryFormatter();
48.
49. properties =
(LinkedList<RealEstateProperty>)bfmProperties.Deserialize(stmProperties
);
50. stmProperties.Close();
51. stmProperties.Close();
52. }
53.
54. // Create a real estate property using the property number that
the user double-clicked
55. RealEstateProperty rep = new RealEstateProperty();
56. string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
57. rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;
58.
59. // Ask the compiler to locate that property
60. LinkedListNode<RealEstateProperty> nodProperty =
properties.Find(rep);
61.
62. // Just in case, make sure the property was found
63. if (nodProperty != null)
64. {
65. // Present a warning message to the user
66. if( MessageBox.Show("Are you sure you want to delete this
property?",
67. "Altair Realtors",
68. MessageBoxButtons.YesNoCancel,
69. MessageBoxIcon.Question) ==
System.Windows.Forms.DialogResult.Yes )
70. {
71. // If the user clicks yes, delete the property
72. properties.Remove(nodProperty);
73.
74. // Save the list of properties
75. string strFilename = dirInfo.FullName +
"\\properties.atr";
76. FileStream stmProperties = new FileStream(strFilename,
77. FileMode.Create,
78.
FileAccess.Write);
79. BinaryFormatter bfmProperty = new BinaryFormatter();
80.
81. bfmProperty.Serialize(stmProperties, properties);
82. stmProperties.Close();
83.
84. // Show the new list of properties
85. ShowProperties();
86. }
87. }
}
88.Return to the form and double-click the Close button
89. Implement it as follows:
90. private void btnClose_Click(object sender, EventArgs e)
91. {
92. Close();
}
93. To execute, press F5
94. Right-click one of the records and click Delete Property
95. Click No
96. Right-click another row and click Delete Property
97. Click Yes
98. Close the form and return to your programming environment

You might also like