Professional Documents
Culture Documents
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.
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...
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 #:
Label Address:
TextBox txtAddress Public
Label City:
Label State:
Label Stories:
Label Condition:
Excellent
ComboBox cbxConditions DropDownList Good Shape Public
Needs Fixing
Label Bedrooms:
Label Bathrooms:
Sale
Label
Status:
Unspecified
ComboBox cbxSaleStatus DropDownList Available Public
Sold
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
Label Property #:
TextBox txtPropertyNumber
Property
Label
Type:
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:
Market
Label
Value:
TextBox txtStatus
BorderStyle:
PictureBox pbxProperty FixedSingle
SizeMode: Zoom