コード例 #1
0
        private void buttonAddDemo_Click(object sender, EventArgs e)
        {
            bool   last = false; // inital values for comboboxes
            String gend = "Male";
            String yr   = "5";

            // check if last year
            if (comboBoxLast.SelectedIndex == 0)
            {
                last = true;
            }

            // check gender
            if (comboBoxGender.SelectedIndex == 1)
            {
                gend = "Female";
            }
            else if (comboBoxGender.SelectedIndex == 2)
            {
                gend = "Other";
            }

            // year of study
            if (comboBoxYear.SelectedIndex < 4) // else it is 5+ (inital value)
            {
                yr = (comboBoxYear.SelectedIndex + 1).ToString();
            }

            // fill in multiline textboxes
            List <String> en = new List <string>();

            en.AddRange(textBoxEnrolled.Lines);
            en.RemoveAll(String.IsNullOrWhiteSpace);
            List <String> ex = new List <string>();

            ex.AddRange(textBoxPrevious.Lines);
            List <String> pr = new List <string>();

            pr.AddRange(textBoxPreffered.Lines);

            bool fail = false;

            foreach (string s in en)
            {
                if (!Regex.Match(s.ToUpper(), "^(CGRD|COMP|STAT|MATH)\\d{3}-\\d{2}(A|B|T|S|C)").Success || s.Equals("") || s.Equals("\n"))
                {
                    MessageBox.Show("Invalid Course: " + s + "\nFORMAT: \"COMP103-14A\"");
                    fail = true;
                }
            }
            if (fail == false && (textBoxFirstName.Text == "" || textBoxFamilyName.Text == ""))
            {
                MessageBox.Show("Name fields cannot be blank");
                fail = true;
            }

            if (fail == false && (textBoxEmail.Text == "" && textBoxPhone.Text == ""))
            {
                MessageBox.Show("At least one form of contact is required, either email or phone");
                fail = true;
            }

            if (fail == false)
            {
                try
                {
                    int.Parse(textBoxAge.Text);
                }
                catch (Exception exc)
                {
                    MessageBox.Show("Age needs to be a number");
                    fail = true;
                }
            }

            // If invalid Paper Don't save
            if (fail)
            {
                return;
            }

            // Create new demo
            Demo d = new Demo(textBoxFirstName.Text, textBoxFamilyName.Text, textBoxPhone.Text, textBoxAge.Text, gend, textBoxUsername.Text, textBoxID.Text, textBoxSummer.Text, textBoxMajor.Text, textBoxDegree.Text, yr, last, textBoxEmail.Text, en, ex, pr, times);

            if (editing)      // if the demo is being edited
            {
                d.updateDB(); // update demos attribute to database
            }
            else
            {
                Main.potentialDemos.Add(d); // add in demo to main form's list
                d.addToDB();                // add demo to database
            }

            mainForm.ListBoxPotentialDemos_Refresh();
            mainForm.ListBoxPDHours_Refresh();
            // update list box
            this.Close();
        }