Exemple #1
0
        public void ReadFile(InvoiceCollection invoices)
        {
            StreamReader infile;
            char         delimiter = ',';
            string       line;

            string[] fields = new string[4]; // unnecessary assignment of value
            if (File.Exists("invoices.txt"))
            {
                MessageBox.Show("File 'invoices.txt' exists.\n");
            }
            {
                infile = File.OpenText("invoices.txt");
                while (!infile.EndOfStream)
                {
                    line   = infile.ReadLine();
                    fields = line.Split(delimiter);
                    Invoice anInvoice = new Invoice();
                    anInvoice.number      = int.Parse(fields[0]);
                    anInvoice.custID      = int.Parse(fields[1]);
                    anInvoice.date        = DateTime.Parse(fields[2]);
                    anInvoice.salesAmount = double.Parse(fields[3]);
                    invoices.AddInvoice(anInvoice);
                }
                infile.Close();
            }
        }
        private void BtnAddSave_Click(object sender, EventArgs e)
        {
            if (btnAddSave.Text == "Add")
            {
                ReadOnlyInputs(false);
                dtpDate.Enabled = true;
                btnAddSave.Text = "Save";
                btnRemove.Text  = "Cancel";
                cboInvoice.Text = string.Empty;
                txtCustID.Clear();
                txtAmount.Clear();
                cboInvoice.Focus();
            }
            else
            {
                try
                {
                    btnRemove.Text = "Remove";
                    if (txtCustID.Text != string.Empty && txtAmount.Text != string.Empty &&
                        cboInvoice.Text != string.Empty && dtpDate.Text != string.Empty)
                    {
                        Invoice newInvoice = new Invoice();
                        newInvoice.number      = int.Parse(cboInvoice.Text);
                        newInvoice.custID      = int.Parse(txtCustID.Text);
                        newInvoice.date        = dtpDate.Value;
                        newInvoice.salesAmount = double.Parse(txtAmount.Text);
                        anInvoiceCollection.AddInvoice(newInvoice);
                        FillCombo();
                        cboInvoice.Text = string.Empty;
                        txtCustID.Text  = string.Empty;
                        txtAmount.Text  = string.Empty;

                        ReadOnlyInputs(true);
                        dtpDate.Enabled = true;
                        btnAddSave.Text = "Add";
                    }
                    else
                    {
                        MessageBox.Show("Data missing", "Error");
                        btnAddSave.Text = "Save";
                        btnRemove.Text  = "Cancel";
                    }
                }
                catch
                {
                    MessageBox.Show("Duplicate number", "Error");
                    btnAddSave.Text = "Save";
                    btnRemove.Text  = "Cancel";
                }
            }
        }