Пример #1
0
        private void btnSet_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsValidData())
                {
                    // Saves the values from the five user inputs to the SetCourses Class (First Custom Class)
                    SetCourses.setNumberOfCourses(txtNoCourses.Text);
                    SetCourses.setName(txtStudentName.Text);
                    SetCourses.setDate(startDateCalendar.SelectionStart.ToShortDateString());
                    SetCourses.setCourseNo(cboCourseNo.Text);
                    if (rdoHybrid.Checked)
                    {
                        SetCourses.setCourseType(rdoHybrid.Text);
                    }
                    else
                    {
                        SetCourses.setCourseType(rdoOnline.Text);
                    }
                }
            }

            // Checks for valid data, if data is not present or not valid, an error message box will appear
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "/n/n" +
                                ex.GetType().ToString() + "/n" +
                                ex.StackTrace, "Exception");
            }

            bool IsValidData()
            {
                return
                    (IsPresent(txtNoCourses, "Number of Courses") &&
                     IsInt32(txtNoCourses, "Number of Courses") &&
                     IsWithinRange(txtNoCourses, "Number of Courses", 1, 5) &&

                     IsPresent(txtStudentName, "Student Name"));
            }

            // Verifies the user entered data in the first two text boxes
            bool IsPresent(TextBox textBox, string name)
            {
                if (textBox.Text == "")
                {
                    MessageBox.Show(name + " is a required field.", "Entry Error");
                    textBox.Focus();
                    return(false);
                }
                return(true);
            }

            // Verifies the Number of Courses user input is a valid integer
            bool IsInt32(TextBox textBox, string name)
            {
                int number = 0;

                if (Int32.TryParse(textBox.Text, out number))
                {
                    return(true);
                }
                else
                {
                    MessageBox.Show(name + " must be an integer.", "Entry Error");
                    textBox.Focus();
                    return(false);
                }
            }

            //Verifies the Number of Courses user input is between 1 and 5
            bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)
            {
                decimal number = Convert.ToDecimal(textBox.Text);

                if (number < min || number > max)
                {
                    MessageBox.Show(name + " must be between " + min + " and " + max
                                    + ". Student must have administrative permission to take more than five classes.",
                                    "Entry Error");
                    textBox.Focus();
                    return(false);
                }
                return(true);
            }
        }
 public static string getCourseType2()
 {
     return(SetCourses.getCourseType());
 }
 public static string getCourseNo2()
 {
     return(SetCourses.getCourseNo());
 }
 public static string getDate2()
 {
     return(SetCourses.getDate());
 }
 public static string getName2()
 {
     return(SetCourses.getName());
 }
 // Retrieves stored values from SetCourses Class
 public static int getNumberOfCourses2()
 {
     return(SetCourses.getNumberOfCourses());
 }