using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

namespace ch10_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private FileStream output;
        private FileStream input;
        private BinaryFormatter formatter = new BinaryFormatter();
        BinaryFormatter reader = new BinaryFormatter();
        private void openFile()
        {
            if (File.Exists("student.dat"))
                output = new FileStream("student.dat", FileMode.Append, FileAccess.Write);
            else
                output = new FileStream("student.dat", FileMode.Create, FileAccess.Write);
        }
        //**********************
        private void clearTextBox()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFile();
            Record record = new Record();
            try
            {
                record.StNo = System.Convert.ToInt32(textBox1.Text);
                record.fName = textBox2.Text;
                record.lName = textBox3.Text;
                record.Grade = System.Convert.ToSingle(textBox4.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid Fromat");
                output.Close();
                return;
            }
            formatter.Serialize(output, record);
            output.Close();
            clearTextBox();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                input = new FileStream("student.dat", FileMode.Open, FileAccess.Read);
                textBox5.Text = " Student are :";
                while (true)
                {
                    Record record = (Record)reader.Deserialize(input); ;
                    string result = record.StNo.ToString() + "\t" + record.fName + "\t" + record.lName + "\t" + record.Grade.ToString();
                    textBox5.Text += "\r\n" + result;
                }
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("File not exists");
                return;
            }
            catch (SerializationException)
            {
                input.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                input = new FileStream("student.dat", FileMode.Open, FileAccess.Read);

                textBox5.Text = " Student are grade > 17 :";
                while (true)
                {
                    Record record = (Record)reader.Deserialize(input); ;
                    if (record.Grade > 17)
                    {
                        string result = record.StNo.ToString() + "\t" + record.fName + "\t" + record.lName + "\t" + record.Grade.ToString();
                        textBox5.Text += "\r\n" + result;
                    }
                }
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("File not exists");
                return;
            }
            catch (SerializationException)
            {
                input.Close();
            }
        }
    }
}
