示例#1
0
        // Tells te user that they need admin permissions
        private void adminForm()
        {
            notificationForm nF = new notificationForm("You need admin permissions to access this screen");

            nF.ShowDialog();
            SystemSounds.Beep.Play();
        }
        // Editing actions to take on double click
        private void content_DoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (checkoutGrid.CurrentCell != null && checkoutGrid.CurrentCell.Value != null)
            {
                // If the last column
                if (checkoutGrid.CurrentCell.ColumnIndex.Equals(7) && e.RowIndex != -1)
                {
                    if (instrument == null)
                    {
                        noteEntry noteView = new noteEntry(studentCheckouts[checkoutGrid.CurrentCell.RowIndex].note, studentCheckouts[checkoutGrid.CurrentCell.RowIndex].instrument.name, checkoutGrid.CurrentCell.RowIndex);
                        noteView.noteTextBox.Enabled = false;
                        noteView.noteLabel.Text      = "";
                        noteView.saveButton.Hide();
                        noteView.cancelButton.Location = new Point(110, 260);
                        noteView.ShowDialog();
                    }
                    else
                    {
                        // View note
                        noteEntry noteView = new noteEntry(instrument.checkouts[checkoutGrid.CurrentCell.RowIndex].note, instrument.name, checkoutGrid.CurrentCell.RowIndex);
                        noteView.ShowDialog();
                    }
                }
                // If the first column
                else if (instrument != null && (checkoutGrid.CurrentCell.ColumnIndex.Equals(0) && e.RowIndex != -1) && Form1.currentEmployee != null)
                {
                    // Initializes and assigns variables for the starting and ending
                    // values of the transaction type
                    string cStart = "Check Out";
                    string cEnd   = "Check In";

                    // Doing it this way saves two stirng parses

                    // If it was originally a check in, it sets it to a check out
                    switch (checkoutGrid.CurrentCell.Value.ToString())
                    {
                    case "Check In":
                        cStart = "Check In";
                        cEnd   = "Check Out";
                        break;
                    }

                    // Assigns cell text value
                    checkoutGrid.CurrentCell.Value = cEnd;

                    // Notification
                    notificationForm nF = new notificationForm(cStart, cEnd);
                    nF.ShowDialog();
                    nF.Refresh();
                }
            }
        }
        // Adds an employee name to the list
        private void addButton_Click(object sender, EventArgs e)
        {
            passwordCreated = false;
            if (original.Count > 0 | adminButton.Checked)
            {
                if (nameBox.Text != "" && nameBox.Text != "Enter an employee name")
                {
                    // Capitalize the name
                    nameBox.Text = Checkout_Data.capitalizeName(nameBox.Text);
                    Employees temp = new Employees(nameBox.Text);
                    if (adminButton.Checked)
                    {
                        AddPassword aP = new AddPassword(temp);
                        aP.ShowDialog();
                    }
                    else
                    {
                        passwordCreated = true;
                    }
                    // Add new employee to the list
                    if (passwordCreated)
                    {
                        Form1.currentStaff.Add(temp);
                    }

                    passwordCreated = false;

                    nameBox.Text = "";
                    // Sort datasource
                    employeeBox.DataSource = Form1.currentStaff.OrderBy(x => x.eName.Substring(x.eName.LastIndexOf(" ") + 1))
                                             .ToList <Employees>();
                    Refresh();
                }
            }
            else
            {
                notificationForm nF = new notificationForm("You have to create an admin account\nbefore leaving this screen.");
                nF.ShowDialog();
            }
        }
        // When cell is done being edited
        private void checkoutGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (Form1.currentEmployee != null)
            {
                // If the string in the first column is "Check In"
                if ((string)checkoutGrid[0, selectedIndex[1]].Value == "Check In")
                {
                    cBackward = 0;
                }
                else
                {
                    cBackward = (Checkout.Type) 1;
                }

                foreach (Instrument nInstrument in Form1.allInstruments)
                {
                    if (nInstrument.id == instrument.id)
                    {
                        // Save information to the instrument
                        nInstrument.checkouts[selectedIndex[1]].type  = cBackward;
                        nInstrument.checkouts[selectedIndex[1]].sName = (string)checkoutGrid[1, selectedIndex[1]].Value.ToString();
                        int temp1;

                        // Makes sure that the user inputed an actual intger
                        if (!Int32.TryParse(checkoutGrid[2, selectedIndex[1]].Value.ToString(), out temp1) | temp1.ToString().Length != 7)
                        {
                            checkoutGrid[2, selectedIndex[1]].Value = nInstrument.checkouts[selectedIndex[1]].sID;
                            notificationForm nF = new notificationForm("You must enter a 7-digit, number-only input");
                            nF.ShowDialog();
                            nF.Refresh();
                        }
                        else
                        {
                            nInstrument.checkouts[selectedIndex[1]].sID = Int32.Parse(checkoutGrid[2, selectedIndex[1]].Value.ToString());
                        }

                        nInstrument.checkouts[selectedIndex[1]].emailAddress = (string)checkoutGrid[3, selectedIndex[1]].Value.ToString();

                        DateTime temp2;

                        // Makes sure that the user inputed an actual date
                        if (!DateTime.TryParse(checkoutGrid[4, selectedIndex[1]].Value.ToString(), out temp2))
                        {
                            checkoutGrid[4, selectedIndex[1]].Value = nInstrument.checkouts[selectedIndex[1]].date;
                            notificationForm nF = new notificationForm("You must enter a correct date format");
                            nF.ShowDialog();
                            nF.Refresh();
                        }
                        else
                        {
                            nInstrument.checkouts[selectedIndex[1]].date = checkoutGrid[4, selectedIndex[1]].Value.ToString();
                        }

                        nInstrument.checkouts[selectedIndex[1]].staff    = (string)checkoutGrid[5, selectedIndex[1]].Value.ToString();
                        nInstrument.checkouts[selectedIndex[1]].semester = (string)checkoutGrid[6, selectedIndex[1]].Value.ToString();

                        // Update the instrument variable in this class
                        instrument = nInstrument;

                        // Sort out instruments with the new value
                        Form1.sortInstruments();
                        break;
                    }
                }
            }
        }
        // Contains all of the code for this class
        public static void export(string filepath)
        {
            // List of all instruments
            List <Instrument> holderList = new List <Instrument>();

            // Add all instruments to the holderlist in sorted order
            foreach (BindingList <Instrument> list in Form1.libraryRecords)
            {
                if (list != null)
                {
                    holderList.AddRange(list.OrderBy(x => x.id).ToList());
                }
            }


            // Filepath for export
            //string filepath = Form1.filepath + "\\exportedLibrary.csv";

            // Delete previous file in filepath if it exists
            try
            {
                File.Delete(filepath);
            }
            catch (Exception)
            {
                // Skips the rest because there's no point in going on if this doesn't work
                goto END;
            }

            StringBuilder csvContent = new StringBuilder();

            // Main header for csv file
            string mainHeader = "Catagory,Cabinate,Name,Chapman ID #,Bow #,Brand,Model,Vendor,Serial #,Value,Status,Notes,";

            // Transaction header
            string checkoutFiller = "In/Out,Name,ID,Email,Date,Staff,Semester,Notes";

            int checkoutNumber = 0;

            // Figures out the max number of transactions in the file
            foreach (Instrument instrument in holderList)
            {
                if (instrument.checkouts.Count > checkoutNumber)
                {
                    checkoutNumber = instrument.checkouts.Count;
                }
            }

            // Initialize transaction header
            string checkoutList = "Transaction 1," + checkoutFiller;

            // For every other checkout, add to the header
            for (int i = 2; i <= checkoutNumber; i++)
            {
                checkoutList += "," + "Transaction " + i.ToString() + "," + checkoutFiller;
            }

            // Add to csv being built
            csvContent.AppendLine(mainHeader + checkoutList);

            foreach (Instrument instrument in holderList)
            {
                string tempIString;

                // Replace char to prevent the note from messing up the csv file
                string iNote = instrument.note.Replace('\n', ' ').Replace(',', ' ');

                // Create string with data
                tempIString = instrument.type.ToString() + "," +
                              instrument.cabinate + "," +
                              instrument.name + "," +
                              instrument.id + "," +
                              instrument.bow + "," +
                              instrument.brand + "," +
                              instrument.model + "," +
                              instrument.vendor + "," +
                              instrument.serialNumber + "," +
                              instrument.value + "," +
                              instrument.status.ToString() + "," +
                              iNote;
                // Append transaction data to tempIString
                foreach (Checkout checkout in instrument.checkouts)
                {
                    // Replace char to prevent the note from messing up the csv file
                    string qNote = checkout.note.content.Replace('\n', ' ').Replace(',', ' ');

                    // Create string with data
                    tempIString += ",," + checkout.type.ToString() + "," +
                                   checkout.sName + "," +
                                   checkout.sID.ToString() + "," +
                                   checkout.emailAddress + "," +
                                   checkout.date + "," +
                                   checkout.staff + "," +
                                   checkout.semester + "," +
                                   qNote;
                }
                // Add to cell
                csvContent.AppendLine(tempIString);
            }
            try
            {
                // Write to the file
                File.AppendAllText(filepath, csvContent.ToString());
            }
            catch (Exception)
            {
                // Error message
                notificationForm not = new notificationForm("There was an error with permissions on your computer\n" +
                                                            "We were not able to write to the file because we\n" +
                                                            "didn't have permission to access it");

                not.ShowDialog();
            }
            END :;
        }