/// <summary>
        /// This method saves information of selected product
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _clickSaveToolStripMenuItem(object sender, EventArgs e)
        {
            DialogResult result;
            string       filename;

            SaveProductFileDialog.Filter           = "Text Files (*.txt)|*.txt|All files (*.*)|*.*";
            SaveProductFileDialog.FileName         = "Product.txt";
            SaveProductFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
            result = SaveProductFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                filename = SaveProductFileDialog.FileName;
                try
                {
                    Stream objStream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                    _objBinaryFormatter.Serialize(objStream, _selectedRowData);
                    objStream.Close();
                    MessageBox.Show("File Written Successfully", "File Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                    MessageBox.Show("Error Writting File", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Beispiel #2
0
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filename;

            SaveProductFileDialog.Filter           = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            SaveProductFileDialog.FileName         = "Product.txt";
            SaveProductFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
            if (SaveProductFileDialog.ShowDialog() == DialogResult.OK)
            {
                filename = SaveProductFileDialog.FileName;
                try
                {
                    this._writer = new StreamWriter(filename);
                    this._writer.WriteLine(ProductIdTextBox.Text);
                    this._writer.WriteLine(ConditionTextBox.Text);
                    this._writer.WriteLine(CostTextBox.Text);
                    this._writer.WriteLine(PlatformTextBox.Text);
                    this._writer.WriteLine(OSTextBox.Text);
                    this._writer.WriteLine(ManufacturerTextBox.Text);
                    this._writer.WriteLine(ModelTextBox.Text);
                    this._writer.WriteLine(MemoryTextBox.Text);
                    this._writer.WriteLine(LCDSizeTextBox.Text);
                    this._writer.WriteLine(HDDTextBox.Text);
                    this._writer.WriteLine(CPUBrandTextBox.Text);
                    this._writer.WriteLine(CPUNumberTextBox.Text);
                    this._writer.WriteLine(GPUTypeTextBox.Text);
                    this._writer.WriteLine(CPUTypeTextBox.Text);
                    this._writer.WriteLine(CPUSpeedTextBox.Text);
                    this._writer.WriteLine(WebCamTextBox.Text);
                    this._writer.Close();
                    MessageBox.Show("File Written Successfully", "File Status",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.Message);
                    MessageBox.Show("Error  Writing File", "File Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        // save to the file
        private void _saveProductFile(Object sender, EventArgs e)
        {
            // open the save file dialog box
            this.SaveProductFileDialog.InitialDirectory = Directory.GetCurrentDirectory();  // set the intial directory
            DialogResult result = SaveProductFileDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                // Declare the StreamWriter Object
                StreamWriter fileStreamWriter = null;

                try
                {
                    // Open the file stream for writing
                    fileStreamWriter = new StreamWriter(this.SaveProductFileDialog.FileName);

                    // loop through each element inside the _ProductData collection to write it to the file
                    foreach (KeyValuePair <string, string> productDetail in this._ProductData)
                    {
                        // write the product detail into file. Example: "productID 7"
                        fileStreamWriter.WriteLine(productDetail.Key + ":" + productDetail.Value);
                    }
                }
                catch (Exception ex)
                {
                    // Give an error message
                    MessageBox.Show("Error saving the file. Please try again!", "Error Saving File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Debug.WriteLine(ex); // print required error information to the console
                }
                finally
                {
                    if (fileStreamWriter != null)
                    {
                        // Close the stream and release the resources
                        fileStreamWriter.Close();
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Event handler to handle click on the tool strip menu items
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string            filename;
            ToolStripMenuItem item = sender as ToolStripMenuItem;

            switch (item.Name)
            {
            case "selectAnotherProductToolStripMenuItem":
                this.Hide();
                this.previousForm.Show();
                break;

            case "openToolStripMenuItem":
                OpenProductFileDialog.Filter           = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                OpenProductFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
                OpenProductFileDialog.FileName         = "Product.txt";
                if (OpenProductFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filename = OpenProductFileDialog.FileName;
                    try
                    {
                        this._reader = new StreamReader(filename);

                        if (this._reader.Peek() != -1)
                        {
                            ProductIdTextBox.Text     = this._reader.ReadLine();
                            ConditionTextBox.Text     = this._reader.ReadLine();
                            CostTextBox.Text          = this._reader.ReadLine();
                            PlatformTextBox.Text      = this._reader.ReadLine();
                            OSTextBox.Text            = this._reader.ReadLine();
                            ManufacturerTextBox.Text  = this._reader.ReadLine();
                            ModelTextBox.Text         = this._reader.ReadLine();
                            MemoryTextBox.Text        = this._reader.ReadLine();
                            LCDSizeTextBox.Text       = this._reader.ReadLine();
                            HDDTextBox.Text           = this._reader.ReadLine();
                            CPUBrandTextBox.Text      = this._reader.ReadLine();
                            CPUNumberTextBox.Text     = this._reader.ReadLine();
                            GPUTypeTextBox.Text       = this._reader.ReadLine();
                            CPUTypeTextBox.Text       = this._reader.ReadLine();
                            CPUSpeedTextBox.Text      = this._reader.ReadLine();
                            WebCamTextBox.Text        = this._reader.ReadLine();
                            ServiceClass.selectedId   = Convert.ToInt32(ProductIdTextBox.Text);
                            ServiceClass.condition    = ConditionTextBox.Text;
                            ServiceClass.cost         = Convert.ToDecimal(CostTextBox.Text);
                            ServiceClass.platform     = PlatformTextBox.Text;
                            ServiceClass.os           = OSTextBox.Text;
                            ServiceClass.manufacturer = ManufacturerTextBox.Text;
                            ServiceClass.model        = ModelTextBox.Text;
                            ServiceClass.ram          = MemoryTextBox.Text;
                            ServiceClass.lcdSize      = LCDSizeTextBox.Text;
                            ServiceClass.hdd          = HDDTextBox.Text;
                            ServiceClass.cpuBrand     = CPUBrandTextBox.Text;
                            ServiceClass.cpuNumber    = CPUNumberTextBox.Text;
                            ServiceClass.gpuType      = GPUTypeTextBox.Text;
                            ServiceClass.cpuType      = CPUTypeTextBox.Text;
                            ServiceClass.cpuSpeed     = CPUSpeedTextBox.Text;
                            ServiceClass.webCam       = WebCamTextBox.Text;
                        }
                        else
                        {
                            MessageBox.Show("File Empty - No data to Read", "Error Reading File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                        this._reader.Close();
                    }
                    catch (Exception exception)
                    {
                        Debug.WriteLine(exception.Message);
                        MessageBox.Show("Error Reading File", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                if (ProductIdTextBox.Text == "0")
                {
                    MessageBox.Show("No Data Found!!Please select product from previous form!!", "No Product Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.previousForm.Show();
                    this.Hide();
                }
                break;

            case "saveToolStripMenuItem":
                SaveProductFileDialog.Filter           = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                SaveProductFileDialog.FileName         = "Product.txt";
                SaveProductFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
                if (SaveProductFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filename = SaveProductFileDialog.FileName;
                    try
                    {
                        this._writer = new StreamWriter(filename);
                        this._writer.WriteLine(ProductIdTextBox.Text);
                        this._writer.WriteLine(ConditionTextBox.Text);
                        this._writer.WriteLine(CostTextBox.Text);
                        this._writer.WriteLine(PlatformTextBox.Text);
                        this._writer.WriteLine(OSTextBox.Text);
                        this._writer.WriteLine(ManufacturerTextBox.Text);
                        this._writer.WriteLine(ModelTextBox.Text);
                        this._writer.WriteLine(MemoryTextBox.Text);
                        this._writer.WriteLine(LCDSizeTextBox.Text);
                        this._writer.WriteLine(HDDTextBox.Text);
                        this._writer.WriteLine(CPUBrandTextBox.Text);
                        this._writer.WriteLine(CPUNumberTextBox.Text);
                        this._writer.WriteLine(GPUTypeTextBox.Text);
                        this._writer.WriteLine(CPUTypeTextBox.Text);
                        this._writer.WriteLine(CPUSpeedTextBox.Text);
                        this._writer.WriteLine(WebCamTextBox.Text);
                        this._writer.Close();
                        MessageBox.Show("File Written Successfully", "File Status",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception exception)
                    {
                        Debug.WriteLine(exception.Message);
                        MessageBox.Show("Error  Writing File", "File Error",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                break;

            case "exitToolStripMenuItem":
                this.Close();
                break;
            }
        }
Beispiel #5
0
        /// <summary>
        /// This is handler for save MenuStrip click Event for Save File
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Create Variables
            DialogResult result;
            string       fileName;

            //Assign Properties to SaveDialog
            SaveProductFileDialog.Filter           = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            SaveProductFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
            SaveProductFileDialog.Title            = "Save File";

            //Open DialogBox with the use of ShowDialog Method
            result = SaveProductFileDialog.ShowDialog();

            //If the result is OK then save the File
            if (result == DialogResult.OK)
            {
                fileName = SaveProductFileDialog.FileName;
                try
                {
                    //if Product file is already exist then It will delete it
                    if (File.Exists("Product.txt"))
                    {
                        File.Delete("Product.txt");
                    }
                    //Open a Saved Stream
                    this._writer = new StreamWriter("Product.txt", true);
                    //Write all TextBox values in buffer
                    //Write Product basic Information
                    _writer.WriteLine(ProductIDTextBox.Text);
                    _writer.WriteLine(ConditionTextBox.Text);
                    _writer.WriteLine(CostTextBox.Text);

                    //Write Product basic Information
                    _writer.WriteLine(PlatformTextBox.Text);
                    _writer.WriteLine(ManufacturerTextBox.Text);
                    _writer.WriteLine(OSTextBox.Text);
                    _writer.WriteLine(ModelTextBox.Text);

                    //Write Product Tech Specs Information
                    _writer.WriteLine(MemoryTextBox.Text);
                    _writer.WriteLine(CPUBrandTextBox.Text);
                    _writer.WriteLine(CPUTypeTextBox.Text);
                    _writer.WriteLine(LCDSizeTextBox.Text);
                    _writer.WriteLine(CPUNumberTextBox.Text);
                    _writer.WriteLine(CPUSpeedTextBox.Text);
                    _writer.WriteLine(HDDTextBox.Text);
                    _writer.WriteLine(GPUTypeTextBox.Text);
                    _writer.WriteLine(WebCamTextBox.Text);

                    _writer.Close(); //Flushes to buffers

                    MessageBox.Show("File Written Successfully", "File Status",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.Message);
                    MessageBox.Show("Error Writing File", "File Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }