示例#1
0
文件: Form1.cs 项目: Mdcrab02/CIS199
        //This method really shouldn't allow for duplicate call numbers
        //although it isn't specified in the directions.  I want to make a
        //list of books local to this event, and have an if statement and
        //a for each loop go through each book currently in the library,
        //and check for duplicate call numbers, but I'm out of time I guess
        //I'll have to take a hit on this.
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int pageHolder = 0;
            int callholder = 0;
            //Two local variables used in error checking to make sure both the
            //page number and call number are indeed numbers

            try
            {
                if (string.IsNullOrWhiteSpace(txtAuthorAdd.Text) || string.IsNullOrWhiteSpace(txtCallAdd.Text)
                    || string.IsNullOrWhiteSpace(txtGenreAdd.Text) || string.IsNullOrWhiteSpace(txtPagesAdd.Text)
                    || string.IsNullOrWhiteSpace(txtPubAdd.Text) || string.IsNullOrWhiteSpace(txtTitleAdd.Text))
                    //This is a really rough way of making sure all the textboxes
                    //have something in them
                {
                    MessageBox.Show("Please fill out all fields");
                    //if not, throw up a message box.
                }
                else if ((!int.TryParse(txtPagesAdd.Text, out pageHolder)) ||
                    (!int.TryParse(txtCallAdd.Text, out callholder)))
                    //This then checks to see if the number of pages and
                    //call number are indeed numbers
                {
                    MessageBox.Show("Number of Pages and call number must be a number");
                    //Otherwise, throw up a message box telling the user the error
                }
                else
                {
                    //If all this checks out, add the book to the library
                    Book newbook = new Book(txtCallAdd.Text, txtTitleAdd.Text, txtAuthorAdd.Text,
                        txtGenreAdd.Text, int.Parse(txtPagesAdd.Text), txtPubAdd.Text);
                    //Create a new book based on the information given in the text
                    //boxes

                    myLibrary.mybooks.Add(newbook);
                    //Add the newly created book to our library

                    //Display "book added" message box
                    MessageBox.Show("Book Added to Library!");

                    //Clear fields
                    txtAuthorAdd.Clear();
                    txtCallAdd.Clear();
                    txtGenreAdd.Clear();
                    txtPagesAdd.Clear();
                    txtPubAdd.Clear();
                    txtTitleAdd.Clear();

                    txtAuthorAdd.Focus();
                    //set the focus on the first text box

                    PopulateBooks();
                    //Run the method that updates the lbox on the second
                    //tab so information is updated in real time
                }
            }
            catch
            {
                MessageBox.Show("An error occurred, please try again.");
                //Just in case a non-specified error occurs
            }
        }
示例#2
0
文件: Form1.cs 项目: Mdcrab02/CIS199
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (myLibrary.CallNumberExists(txtCallAdd.Text) == true)
            //This calls the CallNumberExists method from the Library class
            //which returns a bool
            //and checks it with the data entered in the call number txtbox.
            {
                MessageBox.Show("That call number already exists.");
                txtCallAdd.Focus();
                //If the call number exists already, then bring up a message box
                //and let the user know what's up as well as put the focus on that
                //text box
            }
            else
            {
            //If that checks out, move on to the rest of the program.
                int pageHolder = 0;
                int callholder = 0;
                double sizeholder = 0;

                //Two local variables used in error checking to make sure both the
                //page number and call number are indeed numbers

                try
                {
                    if (string.IsNullOrWhiteSpace(txtAuthorAdd.Text) || string.IsNullOrWhiteSpace(txtCallAdd.Text)
                        || string.IsNullOrWhiteSpace(txtGenreAdd.Text) || string.IsNullOrWhiteSpace(txtPagesAdd.Text)
                        || string.IsNullOrWhiteSpace(txtPubAdd.Text) || string.IsNullOrWhiteSpace(txtTitleAdd.Text))
                    //This is a really rough way of making sure all the textboxes
                    //have something in them
                    //Now that I think about it, if i had an array of textboxes this wouldn't
                    //be necessary
                    {
                        MessageBox.Show("Please fill out all fields");
                        //if not, throw up a message box.
                    }
                    else if ((!int.TryParse(txtPagesAdd.Text, out pageHolder)) ||
                        (!int.TryParse(txtCallAdd.Text, out callholder)))
                    //This then checks to see if the number of pages and
                    //call number are indeed numbers
                    {
                        MessageBox.Show("Number of Pages and call number must be a number");
                        //Otherwise, throw up a message box telling the user the error
                    }
                    else if (pageHolder >= 9000)
                    {
                        MessageBox.Show("Over 9000?  Your scouter must be broken!");
                        txtPagesAdd.Focus();
                        //If the user tries to say that the book has more than 9000 pages
                        //tell him to check again.
                    }
                    else
                    {
                        if (chkEbookAdd.Checked == false)
                        {
                            //If all this checks out, add the book to the library
                            Book newbook = new Book(txtCallAdd.Text, txtTitleAdd.Text, txtAuthorAdd.Text,
                                txtGenreAdd.Text, int.Parse(txtPagesAdd.Text), txtPubAdd.Text);
                            //Create a new book based on the information given in the text
                            //boxes
                            myLibrary.mybooks.Add(newbook);
                            //myLibrary.mybooks.Add(newbook);
                            //Add the newly created book to our library

                            //Display "book added" message box
                            MessageBox.Show("Book Added to Library!");

                            //Clear fields
                            txtAuthorAdd.Clear();
                            txtCallAdd.Clear();
                            txtGenreAdd.Clear();
                            txtPagesAdd.Clear();
                            txtPubAdd.Clear();
                            txtTitleAdd.Clear();
                            txtEbookAdd.Clear();
                            chkEbookAdd.Checked = false;

                            txtAuthorAdd.Focus();
                            //set the focus on the first text box

                            PopulateBooks();
                            //Run the method that updates the lbox on the second
                            //tab so information is updated in real time

                        }
                        if (chkEbookAdd.Checked == true)
                        //if the user checks the ebook checkbox, then run some
                        //input value as it relates to ebooks.
                        {
                            if (!double.TryParse(txtEbookAdd.Text, out sizeholder))
                            //If the user tries to enter something that isn't a number
                            {
                                MessageBox.Show("File size must be a number...");
                                txtEbookAdd.Focus();
                                //Show a message box and then put the focus on that text box
                            }
                            else if ((sizeholder <= 0) || (sizeholder >= 10000))
                            //if the user enters a value at 0 or below, or above 10,000MB
                            {
                                MessageBox.Show("The file size must be greater than 0 and less than 10GB");
                                txtEbookAdd.Focus();
                                //Show a messagebox and put the focus on that text box.
                            }
                            else if (string.IsNullOrWhiteSpace(txtEbookAdd.Text))
                            //if the user enters nothing in the text box
                            {
                                MessageBox.Show("You must enter a non zero value for file size.");
                                txtEbookAdd.Focus();
                                //Show a messagebox and put the focus on that text box.
                            }
                            else
                            {
                                Book newbook = new eBook(txtCallAdd.Text, txtTitleAdd.Text, txtAuthorAdd.Text,
                                    txtGenreAdd.Text, int.Parse(txtPagesAdd.Text), txtPubAdd.Text, double.Parse(txtEbookAdd.Text));
                                myLibrary.mybooks.Add(newbook);
                                //This adds a new ebook of type book (because ebook is a child
                                //class of Book and that's what inheritance is all about) and
                               //instantiates it with all of the properties of Book with the
                                //addition of the property for ebook.

                                //Display "book added" message box
                                MessageBox.Show("Book Added to Library!");

                                //Clear fields
                                txtAuthorAdd.Clear();
                                txtCallAdd.Clear();
                                txtGenreAdd.Clear();
                                txtPagesAdd.Clear();
                                txtPubAdd.Clear();
                                txtTitleAdd.Clear();
                                txtEbookAdd.Clear();
                                chkEbookAdd.Checked = false;
                                //uncheck the check box

                                txtAuthorAdd.Focus();
                                //set the focus on the first text box

                                PopulateBooks();
                                //Run the method that updates the lbox on the second
                                //tab so information is updated in real time
                            }

                        }

                    }
                }
                catch
                {
                    MessageBox.Show("An error occurred, please try again.");
                    //Just in case a non-specified error occurs
                }
            }
        }