 |
Creating a Client Server Application |
|
Practical
Learning: Creating Cars
|
|
- To add a new class to the project, in the Class View, right-click
BethedaCarRental1 -> Add -> Class...
- Set the Class Name to Car and click Add
- Change the file as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BethesdaCarRental1
{
[Serializable]
public class Car
{
public string Make;
public string Model;
public int Year;
public string Category;
public bool HasCDPlayer;
public bool HasDVDPlayer;
public bool IsAvailable;
public Car(string mk = "Good", string mdl = "Special",
int yr = 1960, string cat = "Unknown", bool cd = false,
bool dvd = false, bool avl = false)
{
Make = mk;
Model = mdl;
Year = yr;
Category = cat;
HasCDPlayer = cd;
HasDVDPlayer = dvd;
IsAvailable = avl;
}
}
}
- To add a new form to the application, in the Solution Explorer,
right-click BethesdaCarRental1 -> Add -> Windows Forms
- Set the Name to CarEditor and click Add
- Design the form as follows:
 |
Control |
Text |
Name |
Other Properties |
Label |
Text # |
|
|
TextBox |
|
txtTagNumber |
|
Label |
Make: |
|
|
TextBox |
|
txtMake |
|
Label |
Model: |
|
|
TextBox |
|
txtModel |
|
Label |
Year: |
|
|
TextBox |
|
txtYear |
|
Label |
Category: |
|
|
ComboBox |
|
cboCategories |
DropDownStyle:
DropDownList |
Items: |
Economy Compact Standard
Full Size Mini Van SUV Truck Van |
PictureBox |
|
pbxCar |
SizeMode: Zoom |
CheckBox |
CD Player |
chkCDPlayer |
CheckAlign:
MiddleRight |
CheckBox |
DVD Player |
chkDVDPlayer |
CheckAlign:
MiddleRight |
Button |
Select Car Picture... |
btnSelectPicture |
|
CheckBox |
Available |
chkAvailable |
CheckAlign:
MiddleRight |
Label |
Picture Name |
lblPictureName |
|
Button |
Submit |
btnSubmit |
|
Button |
Close |
btnClose |
DialogResult: Cancel |
OpenFileDialog |
(Name): dlgOpen
Title: Select Item Picture DefaultExt: jpg
Filter: JPEG Files (*.jpg,*.jpeg)|*.jpg|GIF Files
(*.gif)|*.gif|Bitmap Files (*.bmp)|*.bmp|PNG Files
(*.png)|*.png |
Form |
|
|
FormBorderStyle:
FixedDialog MaximizeBox: False MinimizeBox:
False ShowInTaskbar: False |
|
- Double-click the Select Car Picture button and implement its event
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 BethesdaCarRental1
{
public partial class CarEditor : Form
{
Dictionary<string, Car> ListOfCars;
public CarEditor()
{
InitializeComponent();
}
private void btnSelectPicture_Click(object sender, EventArgs e)
{
if (dlgPicture.ShowDialog() == DialogResult.OK)
{
lblPictureName.Text = dlgPicture.FileName;
pbxCar.Image = Image.FromFile(lblPictureName.Text);
}
}
}
}
- Return to the Car Editor form
- Double-click an unoccupied area of the form
- Implement the Load event as follows:
private void CarEditor_Load(object sender, EventArgs e)
{
Car vehicle = new Car();
BinaryFormatter bfmCars = new BinaryFormatter();
string FileName = @"\\EXPRESSION\Bethesda Car Rental\Cars.crs";
lblPictureName.Text = ".";
if (File.Exists(FileName))
{
FileStream stmCars = new FileStream(FileName,
FileMode.Open,
FileAccess.Read,
FileShare.Read);
try
{
// Retrieve the list of cars
ListOfCars = (Dictionary<string, Car>)bfmCars.Deserialize(stmCars);
}
finally
{
stmCars.Close();
}
}
else
ListOfCars = new Dictionary<string, Car>();
}
- Return to the Car Editor form
- Double-click the Submit button and
implement its Click event as follows:
private void btnSubmit_Click(object sender, EventArgs e)
{
Car vehicle = new Car();
FileStream stmCars = null;
BinaryFormatter bfmCars = new BinaryFormatter();
string strFilename = @"\\EXPRESSION\Bethesda Car Rental\Cars.crs";
if (txtTagNumber.Text.Length == 0)
{
MessageBox.Show("You must enter the car's tag number");
return;
}
if (txtMake.Text.Length == 0)
{
MessageBox.Show("You must specify the car's manufacturer");
return;
}
if (txtModel.Text.Length == 0)
{
MessageBox.Show("You must enter the model of the car");
return;
}
if (txtYear.Text.Length == 0)
{
MessageBox.Show("You must enter the year of the car");
return;
}
// Create a car
vehicle.Make = txtMake.Text;
vehicle.Model = txtModel.Text;
vehicle.Year = int.Parse(txtYear.Text);
vehicle.Category = cbxCategories.Text;
vehicle.HasCDPlayer = chkCDPlayer.Checked;
vehicle.HasDVDPlayer = chkDVDPlayer.Checked;
vehicle.IsAvailable = chkAvailable.Checked;
// Call the Add method of our collection class to add the car
listOfCars.Add(txtTagNumber.Text, vehicle);
// Save the list
stmCars = new FileStream(strFilename,
FileMode.Create,
FileAccess.Write,
FileShare.Write);
try
{
bfmCars.Serialize(stmCars, listOfCars);
if (lblPictureName.Text.Length != 0)
{
FileInfo flePicture = new FileInfo(lblPictureName.Text);
flePicture.CopyTo(@"\\EXPRESSION\Bethesda Car Rental\" +
txtTagNumber.Text +
flePicture.Extension);
}
txtTagNumber.Text = "";
txtMake.Text = "";
txtModel.Text = "";
txtYear.Text = "";
cbxCategories.Text = "Economy";
chkCDPlayer.Checked = false;
chkDVDPlayer.Checked = false;
chkAvailable.Checked = false;
lblPictureName.Text = ".";
pbxCar.Image = null;
}
finally
{
stmCars.Close();
}
}
- Display the Central form
- Double-click the Cars button and implement its event as follows:
private void btnCars_Click(object sender, EventArgs e)
{
CarEditor dlgCars = new CarEditor();
dlgCars.ShowDialog();
}
- To save the project, on the Standard toolbar, click the Save All
button
|
|