using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace File_serialize_Book
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Book bookObject;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItemFile;
private System.Windows.Forms.MenuItem menuItemSave;
private System.Windows.Forms.MenuItem menuItemRetrieve;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBoxTitle;
private System.Windows.Forms.TextBox textBoxQty;
private System.Windows.Forms.TextBox textBoxPrice;
private System.Windows.Forms.Label labelTotal;
private System.Windows.Forms.Button buttonCompute;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItemFile = new System.Windows.Forms.MenuItem();
this.menuItemSave = new System.Windows.Forms.MenuItem();
this.menuItemRetrieve = new System.Windows.Forms.MenuItem();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBoxTitle = new System.Windows.Forms.TextBox();
this.textBoxQty = new System.Windows.Forms.TextBox();
this.textBoxPrice = new System.Windows.Forms.TextBox();
this.labelTotal = new System.Windows.Forms.Label();
this.buttonCompute = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemFile});
//
// menuItemFile
//
this.menuItemFile.Index = 0;
this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemSave,
this.menuItemRetrieve});
this.menuItemFile.Text = "File";
//
// menuItemSave
//
this.menuItemSave.Index = 0;
this.menuItemSave.Text = "Save Record";
this.menuItemSave.Click += new System.EventHandler(this.menuItemSave_Click);
//
// menuItemRetrieve
//
this.menuItemRetrieve.Index = 1;
this.menuItemRetrieve.Text = "Retrieve Record";
this.menuItemRetrieve.Click += new System.EventHandler(this.menuItemRetrieve_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(64, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Title:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(64, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Quantity:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(64, 120);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 23);
this.label3.TabIndex = 2;
this.label3.Text = "Price:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(64, 160);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 23);
this.label4.TabIndex = 3;
this.label4.Text = "Total:";
//
// textBoxTitle
//
this.textBoxTitle.Location = new System.Drawing.Point(136, 40);
this.textBoxTitle.Name = "textBoxTitle";
this.textBoxTitle.Size = new System.Drawing.Size(160, 20);
this.textBoxTitle.TabIndex = 4;
this.textBoxTitle.Text = "";
//
// textBoxQty
//
this.textBoxQty.Location = new System.Drawing.Point(136, 80);
this.textBoxQty.Name = "textBoxQty";
this.textBoxQty.Size = new System.Drawing.Size(160, 20);
this.textBoxQty.TabIndex = 5;
this.textBoxQty.Text = "";
//
// textBoxPrice
//
this.textBoxPrice.Location = new System.Drawing.Point(136, 120);
this.textBoxPrice.Name = "textBoxPrice";
this.textBoxPrice.Size = new System.Drawing.Size(160, 20);
this.textBoxPrice.TabIndex = 6;
this.textBoxPrice.Text = "";
//
// labelTotal
//
this.labelTotal.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.labelTotal.Location = new System.Drawing.Point(136, 160);
this.labelTotal.Name = "labelTotal";
this.labelTotal.Size = new System.Drawing.Size(160, 23);
this.labelTotal.TabIndex = 7;
//
// buttonCompute
//
this.buttonCompute.Location = new System.Drawing.Point(152, 200);
this.buttonCompute.Name = "buttonCompute";
this.buttonCompute.Size = new System.Drawing.Size(96, 23);
this.buttonCompute.TabIndex = 8;
this.buttonCompute.Text = "Compute Total";
this.buttonCompute.Click += new System.EventHandler(this.buttonCompute_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(312, 241);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonCompute,
this.labelTotal,
this.textBoxPrice,
this.textBoxQty,
this.textBoxTitle,
this.label4,
this.label3,
this.label2,
this.label1});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void buttonCompute_Click(object sender, System.EventArgs e)
{
try
{
//Instantiate a book object and set its properties
bookObject = new Book(textBoxTitle.Text,
int.Parse(textBoxQty.Text),
decimal.Parse(textBoxPrice.Text));
//retrieve Total from the bookObject
labelTotal.Text = bookObject.Total.ToString("N");
}
catch
{
MessageBox.Show("Error in quantity or price entered", "Invalid Data");
}
}
private void menuItemSave_Click(object sender, System.EventArgs e)
{
//Save contents of bookObject
FileStream bookStream = new FileStream("BookFile.txt", FileMode.Create);
BinaryFormatter bookFormatter = new BinaryFormatter();
bookFormatter.Serialize(bookStream, bookObject);
bookStream.Close();
}
private void menuItemRetrieve_Click(object sender, System.EventArgs e)
{
//Retrieve a bookObject from disk
BinaryFormatter bookFormatter = new BinaryFormatter();
FileStream bookStream;
try
{
//Read data and set bookObject's properties
bookStream = new FileStream("Bookfile.txt", FileMode.Open);
bookObject = (Book) bookFormatter.Deserialize(bookStream);
bookStream.Close();
//Put data in form's controls on screen
textBoxTitle.Text = bookObject.Title;
textBoxQty.Text = bookObject.Qty.ToString();
textBoxPrice.Text = bookObject.Price.ToString("N");
labelTotal.Text = bookObject.Total.ToString("C");
}
catch
{
MessageBox.Show("Saved object not found", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}